Exception Handling
Exception Handling
Exception is something which occurred irregularly, Exception is basically something happened which
you’re not expecting. This word is used for all sorts of things that are not usually allowed. Real life
example of exception is you wake-up early on the morning and looking for your mobile charger but you
can’t find it anywhere it is an exception.
In programming exception can be occurred due to different situation, for example wrong user action
(invalid input etc.), trying to access the file which is not available in your disk or the path of the file is not
correct, network loss with server connection and etc. to make your software / application more reliable
we must have to handle the exception because if we don’t handle them the app will crash. Let suppose
we are trying to open a file which doesn’t exist on our memory so it will throw exception that
fileNotFound after throwing that exception our app will crash, but if we handle that exception, we can
show user a message that invalid path / file not found please select correct path, doing so our app will
not crash and user can select a desired file.
Another example of exception is user transferring data using our app and suddenly network connection
goes off, so our app will throw the exception but to avoid this we can print a message that no network
connection found and our app will wait until network connection connects back. Let see how exception
is thrown in runtime environment and it leads to program crash.
In above example we are trying to take an integer number from the user, but user enter invalid data like
it can be a string, character, float, double that’s why exception of InputMissMatchException this
exception break-down our application and we can’t do anything anymore to avoid this situation
exception handling comes into existence.
Exception Handling :
Exception handling is the process of responding to the occurrence of exceptions or we can say one of
the powerful procedures to handle the runtime errors so that the normal flow of the application can be
maintained. There are different ways to handle the exception in java which we will discuss one by one.
1. Try-Catch Block
Try catch block is used to handle the exception. The piece of code that can throw the exception is
written in the try block and exception which can be thrown by the code is catch in the catch block. Let
see an example to understand.
In above program we are taking an integer value from the user, but there is a chance that user enter
non-integer value which will cause InputMissMatchException to avoid this we have write down
sc.nextInt() in try block because it can through exception and we caught that exception in catch-block,
printed out the exception and again write down sc.nextInt() so that user can enter a valid integer value.
So, by using try-catch-block we not only handle the exception but our program is not crashed. Let see
another example when we try to access the file which is not available in our hard-drive or at given
location.
As shown in above example the FileNotFoundException is printed out, but exception is not thrown
because we handle that exception using try-catch block.
Finally Block:
Finally, block is used with try-catch block as well, finally block is used to define the piece of code which
will be executed at end no matter try-block / catch-block is executed or not. Finally, block will be
executed at the end no matter which one try / catch block is executed. Finally, block is used when you
have to do something at the end no matter what happened above. Let suppose your app runs only on
internet and if internet goes off it throws exception but we can catch that exception using catch block
but at the end no matter connection faced problem or not you want to disconnect the connection in
that case you can use finally block to disconnect the internet when app is closed. There are three cases
of try-catch block which are,
Even though the try block throws exception because there is no catch block to handle that exception but
the finally block is executed in that case as well and then the exception is thrown.
throws: this keyword is used to warn that the method of block of code can throw the exception, it is
used with method declaration / method signature. Let see some examples how throw and throws
keyword is used. Let see the user-defined method which will throw the exception.
Here we go we have created a main method and call the user-defined method in try-catch block because
it can throw the exception and inside catch block, we catch that exception and print out the exception as
seen in the below picture,
The user enters the wrong data so the exception is thrown by the method and that exception is handled
in the catch block and printed on console as shown in above example with custom message.
These are some of ways how we can handle the exception to make our application more reliable to
avoid any problem a user can occur at run-time.