Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about throw exceptions? #117

Open
AliverdiSultanli opened this issue Oct 22, 2021 · 5 comments
Open

How about throw exceptions? #117

AliverdiSultanli opened this issue Oct 22, 2021 · 5 comments

Comments

@AliverdiSultanli
Copy link

No description provided.

@AliverdiSultanli
Copy link
Author

@alexeymezenin

@alexeymezenin
Copy link
Owner

@AliverdiSultanli I'm sorry, I don't understand the question.

@AliverdiSultanli
Copy link
Author

@alexeymezenin How we can write code about throw exception with best practice? With try catch or another style?

@alexeymezenin
Copy link
Owner

Laravel automatically converts some types of exceptions to appropriate HTTP errors which is great because we don't need to handle those. With other types of exceptions just use standard try/catch.

@devrkd
Copy link

devrkd commented Dec 28, 2021

There few things which can improve your code while dealing with exceptions.

  1. Always throw specific exceptions, rather than generic Exceptions, and if it's related to HTTP, add status code as well.
  2. Though application-specific exceptions can easily be caught by the simple catch statement with that specific exception type, however, if you dealing with some data dependent on an external source, you might want to also catch Throwable, this will allow you to capture exception like missing array index, and help you to log later the exception to an external system like sentry.
  3. Use finally, with try catch statement, it will help you to execute some cleanup stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants