You are on page 1of 6

Lab 17 03-12-2019

Object Oriented Concepts and Programming


Learning Objectives

1. Be able to practically implement different types of exceptions and handling


them with try catch blocks.
2. Write methods and code that propagate exceptions.
3. Write programmer/user defined exception classes and throwing their
instances.

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.

An application that handles when the code attempts to divide by zero.


Open and run the java class DivideByZeroWithExceptionHandling.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:

Program explains the concept of exceptional handling and throwing an exception


and catching exception. 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?
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");
}
}

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.

2. What is the purpose of declaring exceptions? How do you declare an exception,


and where? Can you declare multiple exceptions in a method declaration?

To inform he caller that method can throw an exception. We declare exception in


method header. Yes, we can declare multiple exceptions.

3. What is the printout of the following code?


public class Test {
public static void main(String[] args) {
try {
int value = 30;
if (value < 40)
throw new Exception("value is too small"); }
catch (Exception ex) {
System.out.println(ex.getMessage()); }
System.out.println("Continue after the catch block"); }
}

value is too small


Continue after the catch block
What would be the printout if the line?
int value = 30; is changed to int value = 50;
Nothing.

4. Suppose that statement2 causes an exception in the following try-catch block:


try { statement1;
statement2;
statement3; }
catch (Exception1 ex1) {
}
catch (Exception2 ex2) {
}
statement4;

Answer the following questions:


Will statement3 be executed?
No
If the exception is not caught, will statement4 be executed?
Yes
If the exception is caught in the catch block, will statement4 be executed?
Yes
If the exception is passed to the caller, will statement4 be executed?
No

You might also like