Java exceptional handling :
Java exceptional handling, In java Exceptions occur during programming resulting in abnormal termination of program.A good code need to handle them rather than allowing them to occur.
So in this blog lets look at this concept and ways of java exceptional handling.
There are three kinds of errors
- Checked Exception
- Unchecked Exception
- Errors
java.lang.Throwable is the root class for exceptions
Checked Exceptions :
Compile time exceptions are called as checked exception.Types of checked exceptions are
- IOException
- FileNotFoundException
- SQLException
Unchecked Exception :
Runtime exceptions are called as Unchecked exception.Types of unchecked exceptions are
- ArithematicException
- NullPointerException
- NumberFormatException
- IndexOutOfBoundsException
- ArrayIndexOutofBoundException
- StringIndexOutOfBoundException
Errors :
Errors can’t be handled with try catch blocks they are irrecoverable.
- StackOverflowError
- OutOfMemoryError
- VirtualMachineError
The five keywords in exception handling are :
- try
- catch
- throw
- throws
- finally
try :
When write a code and expect a exception in the statements you wrote then these statements are to be inside try block.
try{ // Statements }
catch:
The exceptions raised in try block are handled in catch block.
catch(Exception e){ Handle exception and print the reason for exception }
Note :
try and catch block are interlinked you can’t specify try without catch and vice versa.
Now let’s try to code try and catch block
class ExceptionHandling { public static void main(String args[]){ String a = null; try{ System.out.println(a.length()); }catch (NullPointerException e){ System.out.println(e.toString()); } } }
throw :
throw keyword is used to throw a exception implicitly i.e., knowingly throwing a exception.This throw is specified in try block only as we specify all statements
class ExceptionHandling { public static void main(String args[]){ try{ throw new NullPointerException("Error"); }catch (NullPointerException e){ System.out.println(e.toString()); } } }
throws :
throws keyword is used to specify a error you are expecting when creating a method.Let’s create a method calc() and specify the expecting exception by throws keyword
static void calc() throws ArithmeticException{ int c = 1/0; }
As we know 1 divided by 0 returns Arithematic Exception.
class ExceptionHandling { public static void main(String args[]){ try{ calc(); }catch (ArithmeticException e){ System.out.println(e.toString()); } } static void calc() throws ArithmeticException{ int c = 1/0; } }
finally :
What ever might be the exception or even there is no exception occurred this block will get executed after try and catch blocks.
finally { System.out.println("Any way i will be called !!!"); }
lets test the scenario with java exceptional handling
class ExceptionHandling { public static void main(String args[]){ String a = null; try{ System.out.println(a.length()); }catch (NullPointerException e){ System.out.println(e.toString()); } finally { System.out.println("Any way i will be called !!!"); } } }
If you have any queries on java exceptional handling let us know in the comment section below.
Do like and share the tutorial for more interesting java tutorials.