freetimestudys@gmail.com          +91-7097633380
  Login Here                        

single time try-catch:-

1) if we are provide try-catch whenever we are getting Exception the corresponding catch block is matched then the code executed good then we are getting normal flow of execution.
2) If the corresponding catch block is not matched then our program always goes to abnormal termination.
  try
  {
  Risky code
 }
catch ()
{
Alternative code
}

single time try-catch program

class Test
{
public static void main(String[] args)
{
try
{
System.out.println("hi");
System.out.println(10/0);
System.out.println("bye");
}
catch (ArithmeticException e)
{
System.out.println("we are getting ArithematicException");
}
System.out.println("if we provide try-catch");
System.out.println("the rest of the code is executed");
}
};

intput and output

output:hi,we are getting ArithematicException

multiple times try-catch:-

try
{
}
catch ()
{
}
try
{
}
catch ()
{
}

multiple times try-catch:-

class Test
{
static String str;//instance variable default value is :null
public static void main(String[] args)
{
try
{
System.out.println(10/0);
}
catch (ArithmeticException ae)
{
System.out.println("we are getting ArithematicException");
}
try
{
System.out.println(str.length());
}
catch (NullPointerException ne)
{
System.out.println("we are getting nullpointerException");
}
System.out.println("if we provide try-catch");
System.out.println("the rest of the code is executed");
}
};

intput and output

output:we are getting ArithematicException we are getting nullpointerException if we provide try-catch the rest of the code is executed

try with multiple catchs

 try
 {
 }
  catch ()
  {
 }
 catch ()
 {
 }

try with multiple catch program

import java.util.*;
class Test
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("provide the division value");
int n=s.nextInt();
try
{
System.out.println(10/n);
String str=null;
System.out.println(str.length());

}
catch (ArithmeticException ae)
{
System.out.println("good boy zero not allowed geting Exception"+ae);
}
catch (NullPointerException ne)
{
System.out.println("good girl getting Exception"+ne);
}
System.out.println("rest of the code");
}
}

intput and output

output:provide the division value 2 5 good girl getting Exceptionjava.lang.NullPointerException rest of the code

nested try-catch

try
{
try
{
}
catch ()
{
}
}
catch ()
{
}

nested try-catch

import java.util.*;
class Test
{
public static void main(String[] args)
{
try
{
try
{
System.out.println("first try block");
int a=10/0;
System.out.println(a);
}
catch (ArithmeticException ae)
{
System.out.println("first catch block"+ae);
}
try
{
System.out.println("second try block");
int[] a={10,20,30};
System.out.println(a[5]);
}
catch (ArrayIndexOutOfBoundsException aaa)
{
System.out.println("second catch block"+aaa);
}
}
catch (Exception e)
{
System.out.println("main catch"+e);
}
System.out.println("normal flow of execution");
}
}

intput and output

output:first try block first catch blockjava.lang.ArithmeticException: / by zero second try block second catch blockjava.lang.ArrayIndexOutOfBoundsException: 5 normal flow of execution

catch with try-catch

try
{
}
catch ()
{
try
{
}
catch ()
{
}
}

catch with try-catch

import java.util.*;
class Test
{
public static void main(String[] args)
{
try
{
System.out.println(10/0);
}
catch (ArithmeticException ae)
{
try
{
System.out.println(10/0);
}
catch (Exception e)
{
System.out.println(e);
}
}
}}

intput and output

output;java.lang.ArithmeticException: / by zero