Exception Handling in Java
Exception Handling in Java
Here is my collection of 10 Java best practices to write Exception handling code in Java. There have been
both applause and criticism of checked Exception in Java, which is a language feature to force dealing with
Exceptions. In this article, we will look to minimize use of checked Exception and learn when to use checked vs
unchecked exceptions in Java as well.
1) Use Checked Exception for Recoverable error and Unchecked Exception for programming error.
Choosing between checked and unchecked exception is always been confusing for Java programmers. Checked
exceptions ensures that you provide exception handling code for error conditions, which is a way from language to
enforcing you for writing robust code, but same time it also add lots of clutter into code and makes it unreadable.
Also, it seems reasonable to catch exception and do something if you have alternatives or recovery strategies. See
checked vs unchecked exceptions for more information on choosing between checked and RuntimeException in
Java.
first one just says that argument is illegal or incorrect, but second one include both name of argument and its illegal
value which is important to point out cause of error. Always follow this Java best practice, when writing code for
handling exceptions and errors in Java.
That's all on Java best practices to follow while handing Exceptions in Java. Do let us know what practices you follow
while writing Exception handling code in Java.