Exceptions in java are signals which indicate some sort of abnormal condition such as an error. And if exceptions pop up in your program, the execution is terminated abnormally. So everything executes fine, till the point where the exception is triggered.
And if exceptions are so dangerous and can abruptly shut down your program execution is there a way to control or manage it?
Yes, there is, and this technique is known as exception handling.
Think about this, when you have set the size of the array and initialized all the elements for that array what if when you were accessing the elements of that array and the index value you passed was out of range, what will happen?
Yes, you are right BOOM! exception message pops up stating “java.lang.ArrayIndexOutOfBoundsException”
Output:
apple
oranges
pineapple
grapes
java.lang.ArrayIndexOutOfBoundsException: 4
let us discuss another example, with an Arithmetic exception
The output for the above program is "Division by zero exception". Notice, how we handled the exception with a custom message. The exception can be handled by a custom message which is more readable
Just like this exception, there are other exceptions which we are going to soon discuss.
The throwable class is the superclass of all exceptions in the java language this includes Logical mistakes. Exceptions are a subclass of the throwable class.
Throwable is the super class of exceptions and errors, there are two types of exception sub-classes, namely sub-classes of RuntimeException and direct subclasses of Exceptions.
There are two categories of exceptions
- checked exceptions: generally caused by networking error, class not found or database access issues
- unchecked exceptions: triggered during runtime, exceptions include NullPointerExceptions,ArrayIndexOutOfBound or ArithmaticException.
As the name says it all, the RuntimeException occurs during runtime and goes unchecked by the compiler so it comes under unchecked exceptions.
The other subclass of throwable is error, which occurs during some logical error in the JVM. examples of errors includes StackOverFlow error which can occur if the java stack area has no memory to create a new stack frame. since these types of errors are sometimes beyond the control of programmer, it comes under the category of unchecked exceptions.
Try and Catch blocks in Java
Exception causing statements are placed inside try block to handle an exception.
Catch block is used to catch exceptions thrown from the try block.
In the above example the result is 2 if you pass 4 and 2. but if you don't pass a value it throws an exception which is caught by ArrayIndexOutOfBoundsException and produces the custom message.
Passing 0 as the second argument will throw an ArithmeticException and executes the catch block with the custom message.
Passing 3 as first argument and "B" as the second argument will throw NumberFormatException, which prints the custom message "please pass only Integer values".
If you do not write try/catch block to catch exceptions, then that exception is sent to JVM default handler and the program execution terminates abnormally. so it is always good to use try/catch block to handle exceptions to print message which can be easily understood by the user.
No comments:
Post a Comment