Class Notes (1,100,000)
US (480,000)
UW-Madison (5,000)
Lecture 30

COMP SCI 200 Lecture Notes - Lecture 30: Exception Handling, Stack TracePremium


Department
Computer Sciences
Course Code
COMP SCI 200
Professor
Paul Marker
Lecture
30

This preview shows pages 1-2. to view the full 6 pages of the document.
Comp Sci 200 -Lecture 30- Exceptions pt 4
Exceptions
a) What is the output when the user enters “0”?
b) What is the output when the user enters “a”?
Scanner sc = new Scanner(System.in);
try {
int a = sc.nextInt();
System.out.print("Value is " + 10/a + ".");
}
catch(InputMismatchException e) {
System.out.print("Catch Mismatch.");
}
catch(ArithmeticException e) {
System.out.print("Catch Div 0.");
}
finally {
System.out.print("Finally.");
}
System.out.print("Done.");
a) Answer: Catch Div 0.Finally.Done.

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Subscribers Only
b) Answer: Catch Mismatch.Finally.Done.
What is the output when the user enters “0”?
Scanner sc = new Scanner(System.in);
try {
int a = sc.nextInt();
System.out.print("Value is " + 10/a + ".");
}
catch(Exception e) {
System.out.print("Catch.");
}
catch(ArithmeticException e) {
System.out.print("Catch Div 0.");
}
finally {
System.out.print("Finally.");
}
System.out.print("Done.");
Answer: Catch.Finally.Done.
What is the output when the user enters “z”?
Scanner sc = new Scanner(System.in);
You're Reading a Preview

Unlock to view full version

Subscribers Only