Labmanual Lab17 Exceptionhandling C
Labmanual Lab17 Exceptionhandling C
Lab Walkthrough/Demo
An application that attempts to divide by zero.
First we demonstrate what happens when errors arise in an application that does
not use exception handling. Open and run the java class
DivideByZeroWithoutExceptionHandling.java uploaded on google classroom.
Lab Tasks
Task 1:
Open ABC.java class from classroom. Examine the code and predict what will
happen if you compile this program.
Compile the program and examine the compiler error messages. What is the
problem with this code?
ABC class program explains the concept of exceptional handling and throwing an
exception and catching exception.
Output:
What problem do you see??
Task 2
1. Add a throws Exception modifier to the declaration of the method C(). Predict
what will happen when you compile your program with this change.
Compile the program and examine the results. Does your code continue to have
problems?
2. Add a throws Exception modifier to the declaration of the method B(). Predict
what will happen when you compile your program with this change.
Compile the program and examine the results. Does your code continue to have
problems?
3. Predict what will be output when you compile and run your program with the
change described above. Run the program and be prepared to explain your results.
4. In your code for main, add a call to method C() immediately after the calls to
methods A() and B() in the try part of the try-catch block. Predict what will be
output when you compile and run your program with this change. Run the program
and be prepared to explain your results.
5. Change method B() so that it catches any exception thrown by the call to method
C(). When B() catches an exception, it should throw a new exception that it
constructs with the text "B is even more exceptional."
Make this change to your code and predict what will happen when you compile and
run your program. Compile and run it and examine your results.
Finally, in your ABC class modify the C() method by removing the throw statement,
replacing it with the statement System.out.println("in C..."). Predict what will
happen when you run your modified program.
6. Call Lab Instructor over when you are ready to show him your results and explain
them.
Compile and run your modified program and examine your results.
Task 3:
Task 4:
Predict what will be output when you compile and run your program with the
change described above. Run the program and be prepared to explain your
results.
import java.util.*;
import javax.swing.*;
class ABC {
public static void main(String [] args)
{
try {
A();
B();
C();
}
catch(Exception e){
System.out.println("in main...");
System.out.println(e);
} }
public static void A(){
try {
B();
} catch(Exception e) {
System.out.println("in A...");
System.out.println(e);
} }
public static void B()throws Exception{
try {
C();
}
catch(Exception e){
System.out.println(e.getMessage()); //
System.out.println(e);
throw new Exception("B is even more exceptional");
}
}
public static void C()throws Exception{
//throw new Exception("C is exceptional");
System.out.println("in C...");
}
}
Home Exercises:
1. Point out the problem in the following code. Does the code throw any
exceptions?
long value = Long.MAX_VALUE + 1;
System.out.println(value);
No this won’t throws any excepts. The value will overflow to the lower range of
long.