Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday, 11 April 2017

Java Try Catch

 Java try catch block

Java try-catch block is a handler which is used to handle the exception in java programs. In java exception handling try and catch is a block which maintains the normal flow of the program.

It most important topic in exception handling concept for interview. And try and catch block is mostly used in every program when we connect to the database.


Java try catch

try Block in Java

In java try block we write all the statements or code that might throw an exception. In other words, java try block contains problematic statements.

Java try block must be used within the method.

Java try block must be followed by catch block or finally block or both blocks.

Syntax try block with catch block :

try
{
//Statements that might throw an exception
}
catch(Exception_class_Name e)
{}

Syntax try block with finally block :

try
{
//Statements that might throw an exception 
}
finally
{}


catch Block in Java

Java catch block must be associated with try block. Java catch block is used to handle the exception. Java catch block must be used after try block only. You can use single or multiple catch block with single try block in java.

Syntax catch block :

try
{
//Statements that might throw an exception
}
catch(Exception_class_Name1 e)
{}
catch(Exception_class_Name2 ee)
{}


Program Without ExceptionHandling(without try and catch)

In the below example we are not going to use try and catch block. Let's see what happens.

class WithoutTryCatchExample
{
public static void main(String args[])
{
int a = 5/0;//Exception occurs
System.out.println("this will not print");
}


output :  Exception in thread "main" java.lang.ArithmeticException :/ by zero

In the above example, the statement "this will not print" will not execute because an exception occurs before this statements. If the exception occurs at any statement, rest of the statement will not execute.


Program with ExceptionHandling(with try and catch)

Solution of the above example is here. In this example, we are going to use try and catch handler to try to catch the exception and execute the statement.

class TryAndCatchExample
{
public static void main(String args[])
{
try
{
int a = 10/0;//ArithmeticException occurs 
}
catch(ArithmeticException e)
{
System.out.println(e);
System.out.println("this will print");
}
}


output: java.lang.ArithmeticException: / by zero

             this will print


Internal working of try-catch block

First JVM checks whether the exception is handled or not, if an exception is not handled then JVM provide default handler that performs some task. These are
  • Print exception description
  • Print the stack trace
  • Causes the program to terminate 
If the exception is handled by the programmer, the normal flow of the application is maintained and all the statement will execute normally.

Java Multi catch block

As we know, we can use multiple catch block with a single try block. Java multi-catch block is used to perform the different-different task at the occurrence of the different-different exception. 

class MultiCatchBlock
{
public static void main(String args[])
{
try
{
int b[] = new int[5];
b[5] = 20/0;
}
catch(ArithmeticException ae)
{
System.out.println("Task First");
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("Task Second");
}
catch(Exception e)
{
System.out.println("Big Task");
}
}


output : Task First

For example 2 :

class MultiCatchBlock
{
public static void main(String args[])
{
try
{
int b[] = new int[5];
b[5] = 20/0;
}
catch(Exception e)
{
System.out.println("Big Task");
}
catch(ArithmeticException ae)
{
System.out.println("Task First");
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("Task Second");
}
}


output: compile time error

Some Points about catch block

  • Only one exception occurred at a time and at a time only one block is executed.
  • All catch block in java must be ordered from most specific to most general i.e catch for ArithmeticException comes before catch for Exception.
Share:

2 comments:

  1. Thanks for sharing this informative blog java training in OMR

    ReplyDelete
  2. Thanks buddy, Before that I am not able to implement try catch block in java programs. but after that blog i am able to implement try catch in any java programs.....

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate