Java- try catch block example
// Example of Simple try catch block
import java.util.*; // for scanner class
class Test
{
public static void main(String ar[])
{
int a,b,c;
a=12;
Scanner scan=new Scanner(System.in);
System.out.println("Enter value of denominator");
b=scan.nextInt();
try{ // try block where exception can occur
c=a/b; // here exception can fire
System.out.println("after devide result= "+c);
}catch(ArithmeticException e) //to handle exception
{
System.out.println("please don't divide number by zero");
}
System.out.println("ok bye!!"); //This line will execute even if there is any Exception
}
}
//output
/*
C:\JAVA>java Test
Enter value of denominator
5
after devide result= 2
ok bye!!
*/