Exception Handling
Learn how Java handles unexpected situations using try, catch and finally.
Java Code
Study the example and understand what each part does.
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero.");
} finally {
System.out.println("Program finished.");
}
}
}
Java code normally runs using the Java Development Kit on a computer, Android Studio or another Java development environment. This course page presents the code examples without pretending that the browser is running Java directly.
Expected Output / Result
Cannot divide by zero. Program finished.
Finished this lesson?
Mark it complete to update your course progress.