An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

An exception can occur for many different reasons. Following are some scenarios where an exception occurs.

    A user has entered an invalid data.

    A file that needs to be opened cannot be found.

    A network connection has been lost in the middle of communications or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.

Based on these, we have three categories of Exceptions. You need to understand them to know how exception handling works in Java.

    Checked exceptions − A checked exception is an exception that is checked (notified) by the compiler at compilation-time, these are also called as compile time exceptions. These exceptions cannot simply be ignored, the programmer should take care of (handle) these exceptions.

    Unchecked exceptions − An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

    Errors − These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.




Try Catch

A method catches an exception using a combination of the try and catch keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code.

Example -

class A{

public static void main(String []arr){

int a[]={1,2,3,4,5};

for(int i=0; i<10; i++){

try{

System.out.print(a[i]+" ");

}

catch(ArrayIndexOutOfBoundsException e){

System.out.println("Out Of Range");

}

}

}

}

Output -

        1  2  3  4  5  Out Of Range 

        Out Of Range  

        Out Of Range  

        Out Of Range  

        Out Of Range


Try With Maltiple Catch

A try block can be followed by multiple catch blocks.

Example -

class A{

public static void main(String []arr){

int a[]={1,2,3,4,5,6,7,8,9};

int b[]={0,2,0,4};

for(int i=0; i<a.length; i++){

try{

System.out.println(a[i]/b[i]+" ");

}

catch(ArrayIndexOutOfBoundsException e){

System.out.println("Out Of Range");

}

catch(ArithmeticException e){

System.out.println("Divide By Zero");

}

}

}

}

Output -

        Divide By Zero

        1

        Divide By Zero

        1

        Out Of Range

        Out Of Range

        Out Of Range

        Out Of Range

        Out Of Range


Finally

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.

Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

Example -

class A{

public static void main(String []arr){

int a[]={1,2,3,4,5};

int b[]={0,2};

for(int i=0; i<a.length; i++){

try{

System.out.println(a[i]/b[i]+" ");

}

catch(ArithmeticException e){

System.out.println("Divide By Zero");

}

catch(ArrayIndexOutOfBoundsException e){

System.out.println("Out Of Range");

}

finally{

System.out.println("Finally");

}

}

}

}

Output -

        Divide By Zero

        Finally

        1

        Finally

        Out Of Range

        Finally

        Out Of Range

        Finally

        Out Of Range

        Finally