Advertisement



< Prev
Next >



Exception Handling using try-catch block





In order to handle an exception thrown in a program, we can use the try-catch block. A try-catch block is made up of two individual blocks - a try block and a catch block.





The catch block declares a type of exception that it is going to catch and handle. If the declared exception-type in catch block is similar to the type of exception thrown by try block, the exception is caught within this catch block and the program continues its normal execution without ending abruptly.











How to catch an exception using try-catch block?


Exception thrown is an object of a certain type of an exception class. In order to catch an exception of a particular type, we have to make sure that our catch block has declared an exception-type similar to the type of exception that is thrown by the try block.


Let's understand this by an example here.
class TryCatch
{
public static void main(String... ar)
{
int a=10, b=0, c;
try
{
	c=a/b; 			//ArithmeticException i.e. Divide by zero exception is thrown here
	System.out.println(c);
}
catch(ArithmeticException ariExp) //matching ArithmeticExcecption is declared in "catch block"
{
	System.out.println("We have an Exception - "+ariExp); //message related to caught exception is printed
}		

System.out.println("Outside try-catch block");
} 		
}		



Output :


We have an Exception - java.lang.ArithmeticException: / by zero
Outside try-catch block


As you can see, an exception of type ArithmeticException i.e. "Divide by zero" is thrown when a statement enclosed within the try block is executed. This exception is caught by the catch block because it is declared with a matching exception of type ArithmeticException.

As the thrown exception ArithmeticException is caught by the catch block, hence, the program does not end abruptly and it continues to successfully execute the statement next to the try-catch block, which prints a message - "Outside try-catch block".




Advertisement




Mismatch in exception declared and exception thrown





Let us understand this by an example here.
class TryCatch2
{

public static void main(String... ar)
{
TryCatch2 ob = new TryCatch2();
try
{
	ob.meth1(); 	//enclosing the call to meth1() method in try block
}
catch(ArithmeticException e)
{
	System.out.println("ArithmaticException thrown by meth1() method is caught in main() method");
}

} //main method ends


public void meth1()
{
try
{
	System.out.println(100/0); //ArithmeticException is thrown here
}
catch(NullPointerException nullExp) //Non-matching exception-type is declared in this catch block
{
	System.out.println("We have an Exception - "+nullExp);
}
System.out.println("Outside try-catch block");
} //meth1() method ends

}


Output:


ArithmeticException thrown by meth1() method is caught in main() method

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement