Join Regular Classroom : Visit ClassroomTech

Java Coding For Interview

Exception Handling

An exception is a problem that arises during the run time of a program.

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		int a=10,b;
		b=a/0;
		System.out.println(b);
	}
}

Output: Run time Error

Exception in thread “main” java.lang.ArithmeticException: / by zero
at CodeWindow.main(Main.java:10)

In the above example, in the line of 10, we are trying to divide integer variable ‘a’ by zero. But programmatically, it will generate ArithmeticException.

Manage this Exception using try-catch

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		int a=10,b;
		try{
		b=a/0;
		System.out.println(b);
		}
		catch(Exception e){
		    System.out.println("Zero Divison Not allowed");
                    System.out.println(e);
		}
	}
}

Output:
Zero Divison Not allowed
java.lang.ArithmeticException: / by zero

 

Explanation:
The code that may generate any exception should be with in Try Block.
If any exception is generated with in Try block, then it will be passed to catch block.
Then the solution for this exception will be mentioned with in catch block.
If there is no exception in the try block then catch block will not be executed.

Types of Exception in JAVA: (Oracle)

Exception Vs Error
Exception Recoverable
Error Irrecoverable

Checked Exception vs Unchecked Exception
Checked Exception is not a subclass of Runtime Exception
Unchecked Exception is a subclass of Runtime Exception
Both inherits Throwable class.

Different types of exception

NullPointerException

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		try{
		String s=null;
		System.out.println(s.length());
		}
		catch(NullPointerException e){
		    System.out.println(e);
		}
		
	}
}

Output:
java.lang.NullPointerException

Number Format Exception

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		try{
		String s="CodeWindow";
		int x=Integer.parseInt(s);
		}
		catch(Exception e){
		    System.out.println(e);
		}
		
	}
}

Ouput:
java.lang.NumberFormatException: For input string: “CodeWindow”

ArrayIndexOutOfBound

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		try{
		int x[]=new int[10];
		x[11]=80;
		}
		catch(Exception e){
		    System.out.println(e);
		}
	}
}

Output:
java.lang.ArrayIndexOutOfBoundsException: 11

StringIndexOutOfBoundException

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		try{
		String s="CodeWindow";
	    System.out.println(s.charAt(12));
		}
		catch(Exception e){
		    System.out.println(e);
		}
		
	}
}

Output:
java.lang.StringIndexOutOfBoundsException: String index out of range: 12

Example of try-catch-finally

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		int a=10,b;
		try{
		System.out.println("Try Block");
		b=a/10;
		System.out.println(b);
		}
		catch(Exception e){
            System.out.println("Catch Block-Exception Occured.");
		}
		finally{
		    System.out.println("Final Block");
		}
	}
}

If any Exception occurs,
then try –> catch –> finally

If no Exception occurs,
then try–>finally

If I mention try & catch then finally is optional.
But for finally block, try-catch is mandatory.

Example of multiple catch block

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		int a=10,b;
		try{
		System.out.println("Try Block");
		b=a/10;
        String s=null;
        System.out.println(s.length());
		System.out.println(b);
		}
		catch(ArithmeticException e){
            System.out.println("ArithmeticException Catch Block Executed.");
		}
		catch(NullPointerException e){
            System.out.println("NullPointerException Catch Block Executed.");
		}
		
	}
}

try with in catch block

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		int a=10,b;
		try{
		System.out.println("Try Block");
		b=a/0;
		System.out.println(b);
		}
		catch(ArithmeticException e){
            System.out.println("ArithmeticException Catch Block Executed.");
            try{
                String s=null;
            System.out.println(s.length());
            }
            catch(NullPointerException E){
                System.out.println("NullPointerException Catch Block Executed.");
    		}
		}
		
		
	}
}

Output:
Try Block
ArithmeticException Catch Block Executed.
NullPointerException Catch Block Executed.

Nested try block

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) 
	{
		
		try{
		System.out.println("Try Block");
		    try{
                String s=null;
                System.out.println(s.length());
            }
            catch(NullPointerException E){
                System.out.println("NullPointerException Catch Block Executed.");
    		}
    	int a=10,b;
		b=a/0;
		System.out.println(b);
		}
		catch(ArithmeticException e){
            System.out.println("ArithmeticException Catch Block Executed.");
            
		}
		
		
	}
}

throw vs throws:
1. throw is used to throw an exception for a method.
1. throws is used to indicate what exception type may be thrown by a method.

2.throw can not throw multiple exception.
2.throws can declare multiple exceptions.

Example of throw:

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
    public static void Zero(){
        throw new ArithmeticException("Divided by zero");
    }
	public static void main (String[] args) 
	{
	    Zero();
	}
}

Output:
Exception in thread “main” java.lang.ArithmeticException: Divided by zero
at CodeWindow.Zero(Main.java:8)
at CodeWindow.main(Main.java:12)

throw with try-catch

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
    public static void Zero(){
        throw new ArithmeticException("Divided by zero");
    }
	public static void main (String[] args) 
	{
	    try{
	        Zero();
	    }
	    catch(ArithmeticException E){
	        System.out.println("Exception handled.");
	        
	    }
	}
}

Output:
Exception handled.

Recent Posts
Pages