Exception Handling || Java example

 

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

  1. Checked Exception
  2. Unchecked Exception
  3. 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

  1. IOException
  2. FileNotFoundException
  3. SQLException

 

Unchecked Exception :

Runtime exceptions are called as Unchecked exception.Types of unchecked exceptions are

  1. ArithematicException
  2. NullPointerException
  3. NumberFormatException
  4. IndexOutOfBoundsException
  •  ArrayIndexOutofBoundException
  •  StringIndexOutOfBoundException

 

Errors :

Errors can’t be handled with try catch blocks they are irrecoverable.

  1. StackOverflowError
  2. OutOfMemoryError
  3. VirtualMachineError

 

The five keywords in exception handling are :

  1. try
  2. catch
  3. throw
  4. throws
  5. 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());
        }

    }

}

 

java exceptional handling

 

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());
        }

    }

}

 

java exceptional handling

 

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;
    }
}

 

java exceptional handling

 

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 !!!");
        }

    }

}

 

java exceptional handling

 

 

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.

 

Show Buttons
Hide Buttons
Read previous post:
Java Constructor | constructor example

  Constructor is used to initialize objects when they are created and memory is allocated to the object.Every class has...

Close