Professional Documents
Culture Documents
Exception
Handling
Exception
Unexpected conditions
Disrupt normal flow of program
With exception handling, we can develop more robust programs
Exceptions are thrown And can be caught
Programming Errors 3
1 import java.util.Scanner;
2
3 public class ExceptionDemo {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter an integer: ");
7 int number = scanner.nextInt();
8 If an exception occurs on this
9 line, the rest of the lines in the // Display the result
method are skipped and the
10 System.out.println(
program is terminated.
11 "The number entered is " + number);
12 }
13 }
Terminated.
Quotient Program 5
import java.util.Scanner;
public class Quotient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
System.out.println(number1 + " / " + number2 + " is
" +
(number1 / number2));
}
}
Quotient Program
6
import java.util.Scanner;
public class Quotient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
System.out.println(number1 + " / " + number2 + " is
" +
(number1 / number2));
}
}
Quotient Program
7
import java.util.Scanner;
public class Quotient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
System.out.println(number1 + " / " + number2 + " is
" +
(number1 / number2));
}
}
Quotientif Program 8
import java.util.Scanner;
public class QuotientWithIf {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
if (number2 != 0)
System.out.println(number1 + " / " + number2 + "
is " +
(number1 / number2));
else
System.out.println("Divisor cannot be zero ");
}
}
Quotientif Program 9
import java.util.Scanner;
public class QuotientWithMethod {
public static int quotient(int number1, int number2) {
if (number2 == 0) {
System.out.println("Divisor cannot be zero");
System.exit(1);
}
return number1 / number2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
int result = quotient(number1, number2);
System.out.println(number1 + " / " + number2 + " is "
+ result); } }
Exception Types 10
System Errors 11
ClassNotFoundException
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Error VirtualMachineError
ClassNotFoundException
ArithmeticException
IOException
Exception NullPointerException
RuntimeException
IndexOutOfBoundsException
Many more classes
Object Throwable IllegalArgumentException
Catching Exceptions
Call Stack
method3
method2 method2
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Trace a Program Execution 23
Suppose no
exceptions in the
statements
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution 24
try {
statements;
}
catch(TheException ex) {
The final block is
handling ex; always executed
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution 25
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
The Next statement
finalStatements; is executed
}
Next statement;
Trace a Program Execution 26
Next statement;
Trace a Program Execution 27
Next statement;
Trace a Program Execution 28
Next statement;
Trace a Program Execution 29
Next statement;
import java.util.Scanner;
public class QuotientWithException {
public static int quotient(int number1, int number2) {
if (number2 == 0)
30
throw new ArithmeticException("Divisor cannot be zero");
return number1 / number2;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter two integers
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
try {
int result = quotient(number1, number2);
System.out.println(number1 + " / " + number2 + " is " + result);
}
catch (ArithmeticException ex) {
System.out.println("Exception: an integer " +
"cannot be divided by zero ");
}
System.out.println("Execution continues ...");
}
}
31
Exception-Handling Example
32
Exception-Handling Example
33