Custom Exceptions - Java
While creating custom exception, prefer to create an unchecked, Runtime exception than a checked exception, especially if you know that client is not going to take any reactive action other than logging.
Read more: http://javarevisited.blogspot.com/2014/06/how-to-create-custom-exception-in-java.html#ixzz3nszKB06y
-------------------------------------------------------------------------------------------------------------------------
Read more: http://javarevisited.blogspot.com/2014/06/how-to-create-custom-exception-in-java.html#ixzz3nszKB06y
-------------------------------------------------------------------------------------------------------------------------
RuntimeException are unchecked while Exception are checked (calling code must handle them).
The custom exception should extends RuntimeException if you want to make it unchecked else extend it with Exception.
With unchecked exceptions calling code method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
As the calling method may not handle RuntimeException, one needs to be careful while throwing RuntimeException.
Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.
Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).
--------------------------------------------------------------------------------------------------------
If you are creating your own Exception that is known as custom exception or user-defined exception. Java custom exceptions are used to customize the exception according to user need.
By the help of custom exception, you can have your own exception and message.
Let's see a simple example of java custom exception.
Comments
Post a Comment