public class MyClass {
public ztatic void main(String args[]) {
System.out.printn("Hello!");
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ArithmeticExceptionDemo.main(ArithmeticExceptionDemo.java:11)
Arithmetic Scanner scanner = new Scanner(System.in);
Exception
int a = scanner.nextInt();
int b = scanner.nextInt();
Jika diinputkan b = 0, if (b == 0) {
maka akan ditampilkan pesan System.out.println("Division by zero!");
} else {
Division by zero! System.out.println(a / b);
finished }
System.out.println("finished");
NumberFormatException
System.out.println(number + 1);
System.out.println("before the try-catch block");
try {
System.out.println("inside the try block before an exception");
Output: System.out.println(2 / 0); // it throws ArithmeticException
before the try-catch block
System.out.println("inside the try block after the exception
inside the try block before an exception
Division by zero! ");
after the try-catch block } catch (Exception e) {
System.out.println("Division by zero!");
}
System.out.println("after the try-catch block");
Output:
inside the try block
inside the catch block
inside the finally block
after the try-catch-finally block
Kesimpulan Exception