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

Exception handling #5

Open
sp0rk opened this issue Mar 12, 2017 · 2 comments
Open

Exception handling #5

sp0rk opened this issue Mar 12, 2017 · 2 comments

Comments

@sp0rk
Copy link

sp0rk commented Mar 12, 2017

e.printStackTrace() is not a proper way to handle exceptions. You can get errors because of something you haven't anticipated or hardware malfunction. Try your best to always come up with a way of properly handling exceptions. If you are 100% certain an exception is not possible to be thrown, write a comment explaining why inside of the catch block.
At least that's what I've heard as a conventional way of doing this stuff.

@wzieba
Copy link
Member

wzieba commented Mar 12, 2017

I don't know what exactly do you mean: a lot of methods from Java ME forces me to write them in try catch blocks. Do you think I should do something else in catch?

@sp0rk
Copy link
Author

sp0rk commented Mar 12, 2017

Basically, Java ME forces you to do it because sometimes things can go wrong and then the program will not behave correctly. If, for example you ignore a exception thrown while saving something in your data storage, and user has a full memory, an exception might be thrown. User will not be notified about the fact that something is wrong and will be surprised to notice that the record has not been saved. Logging an exception is useful for developers but useless for the user. If an error is recoverable you should try to do so - ask user if he wants to try again or something.

  1. Avoid empty catch blocks
    Nothing is more worse than empty catch block, because it not just hides the Errors and Exception, but also may leave your object in unusable or corrupt state. Empty catch block only make sense, if you absolutely sure that Exception is not going to affect object state on any ways, but still its better to log any error comes during program execution. This is not a Java best practice, but a most common practice, while writing Exception handling code in Java.

Read more: http://javarevisited.blogspot.com/2013/03/0-exception-handling-best-practices-in-Java-Programming.html#ixzz4b7nH7eZj
Read more: http://howtodoinjava.com/best-practices/java-exception-handling-best-practices/#9

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

2 participants