Mega Code Archive

 
Categories / Java Book / 001 Language Basics
 

0100 Uncaught Exceptions

This small program includes an expression that intentionally causes a divide-by-zero error: public class Main { public static void main(String args[]) { int d = 0; int a = 42 / d; } } Here is the exception generated when this example is executed: java.lang.ArithmeticException: / by zero at Main.main(Exc0.java:4) Here is another version of the preceding program that introduces the same error but in a method separate from main( ): public class Main { static void subroutine() { int d = 0; int a = 10 / d; } public static void main(String args[]) { subroutine(); } } The resulting stack trace from the default exception handler shows how the entire call stack is displayed: java.lang.ArithmeticException: / by zero at Main.subroutine(Exc1.java:4) at Main.main(Exc1.java:7)