try and catch are blocks in Java used for exception handling.
try block:
try block contains the code that may throw an exception.
try
{
// Risky code that may throw an exception
}
catch block:
catch block is used to handle the exception thrown by the try block.
try block.
catch(ExceptionClassType ref_variable)
{
// Code to handle exception
}
try-catch block.
import java.util.Scanner;
public class MainApp1
{
public static void main(String[] args)
{
System.out.println("----- App Started -----");
Scanner sc = new Scanner(System.in);
System.out.println("Enter no 1");
int no1 = sc.nextInt();
System.out.println("Enter no 2");
int no2 = sc.nextInt();
int res = no1 / no2;
System.out.println("Result : "+res);
System.out.println("----- App Finished Successfully -----");
}
}
----- App Started ----- Enter no 1 100 Enter no 2 4 Result : 25 ----- App Finished Successfully -----
----- App Started -----
Enter no 1
100
Enter no 2
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
at MainApp1.main(MainApp1.java:17)
ArithmeticException and the program will terminate abnormally.
----- App Finished Successfully ----- will not be printed as the program is terminated abnormally.
try-catch block.
import java.util.Scanner;
public class MainApp1
{
public static void main(String[] args)
{
System.out.println("----- App Started -----");
Scanner sc = new Scanner(System.in);
try
{
System.out.println("Enter no 1");
int no1 = sc.nextInt();
System.out.println("Enter no 2");
int no2 = sc.nextInt();
int res = no1 / no2;
System.out.println("Result : "+res);
}
catch(ArithmeticException ae)
{
System.out.println("Exception Occured : "+ae);
}
System.out.println("----- App Finished Successfully -----");
}
}
----- App Started ----- Enter no 1 100 Enter no 2 4 Result : 25 ----- App Finished Successfully -----
----- App Started ----- Enter no 1 100 Enter no 2 0 Exception Occured : java.lang.ArithmeticException: / by zero ----- App Finished Successfully -----
try block and handled the ArithmeticException in the catch block.
ArithmeticException, but instead of terminating the program abnormally, it will jump to the catch block and print the exception message.
try-catch block:
try cannot be used alone; it must be followed by catch or finally.
try
{
// Risky code that may throw an exception
}
catch(ExceptionClassType ref_variable)
{
// Code to handle exception
}
try
{
// Risky code that may throw an exception
}
catch(ExceptionClassType ref_variable)
{
// Code to handle exception
}
finally
{
// Code that will always execute (explained later)
}
catch block) contains detailed information about the exception, such as the :
try block, then only program jumps to the catch block, but if there is not exception in try block, then catch block is skipped or will not be executed.
getMessage()
catch (Exception e)
{
System.out.println(e.getMessage());
}
toString()
catch (Exception e)
{
System.out.println(e.toString());
}
printStackTrace()
catch (Exception e)
{
e.printStackTrace();
}
printStackTrace() for debugging and getMessage() or toString() for user-friendly messages.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.