Try Catch In Java Example
Chapter:
Exception Handling
Last Updated:
06-07-2016 04:19:45 UTC
Program:
/* ............... START ............... */
public class TryCatchInJava {
public static void main(String args[]) {
int number1, number2;
try {
number1 = 0;
number2 = 100 / number1;
System.out.println("Try block message");
} catch (ArithmeticException e) {
// catching divide-by-zero error
System.out.println("Error: Division by Zero");
}
System.out.println("Exit from try-catch block in Java.");
}
}
/* ............... END ............... */
Output
Error: Division by Zero
Exit from try-catch block in Java.
Notes:
-
The try block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block.
- Java try block must be followed by either catch or finally block.
- Java catch block is used to handle the Exception.
- A try block can have any number of catch blocks.
Tags
Java Try Catch