Exception handling in Java
Well, just imagine that you wrote a program, let’s say for the division of two numbers. You put all your possible efforts to write the program, and you try compiling it, and it compiles fine. So everything is good till here. Now you try to run the code, and the code runs fine too.
Here also, there is no problem so far. Now you try to test this code for several inputs for the numerator and denominator(since the program is for division), and find that if you put the value of the denominator is zero, the output comes out to be quite unusual. Basically, this is an exception raised. This brings us to understand what is an exception.
Well, basically, the exception is just an unexpected event that occurs in the program, which disturbs the normal flow of the program. In the above example, the division occurs fine, till we divide some number by zero. As soon as we divide by zero, it comes out to be an ArithmeticException.
Let’s now get into the details of the exceptions, but before that, just confirm that you have understood what is an exception, with help of the above example. We are further going to consider more examples in technical terms so that we understand the concept in more depth.
So, since we have seen what is an exception, now we need to see what is the need for exception handling, and how are we going to handle the exceptions.
Well, in the above example, if we create such a program, for the division of two numbers, if the denominator is zero, the program will just throw an exception, and not run in the usual way. In other words, it terminates without caring about the next instructions (if not handled).
Well, this is an unexpected and unwanted termination. We were expecting some fruitful output, but some unexpected termination is all we got.
So, in order to have a graceful termination, or let’s say to make our program not terminate abnormally, we need to handle the exception that has occurred.
So, in the above example, if someone tries to attempt dividing by zero, you can just catch that ok… there was an attempt to divide by zero, and just do something else instead of division, like you can print that “the division by zero is not possible… sorry you can try some another number”, or something like that, and then just end the program.
This did not terminate the program in an abnormal way, but when someone attempted to divide by zero, we just showed some message, and gracefully terminated our program.
When some exception occurs in our program, it is not known what to do next, so it terminates abnormally, without even caring about the next instructions. For example, let’s go a bit technical. If you are trying to do some file operations, for which, you want to open some files using FileInputStream class, and let’s say that the file is not present at that particular location, from where you want to read it.
So, this will result in an exception again, because the file is not present there, and it is a FileNotFoundException. So, we were told in the program to open a file, but we did not tell what to do if the file is not there, hence, the program was abnormally terminated.
Usually what would happen is that you would open the file, read it and then close the file, but here, since the file is not present there, it is not known what to do, so the execution terminates. This is simple and straightforward.
So, by handling the exception, we can make our program terminate gracefully. We can do something like if the FileNotFoundException occurs, print that – No such file at the specified location, or something else, and the program ends, at least not abnormally.
By handling an exception, we are providing an alternative code, that would run if the exception occurs, just so that the termination is graceful.