Department of Information Technology B.
Tech – 3rd Year
Object Oriented Programming Laboratory Manual
Topic: Exception Handling in Java
Subject Code -PCC IT 593
Objective
To understand and implement various mechanisms of Exception Handling in Java
including try, catch, throw, throws, and finally constructs.
Lab Outcomes
After performing this experiment, students should be able to:
• Distinguish between compile-time, run-time, and logical errors.
• Explain the hierarchy of exception classes in Java.
• Write programs using single and multiple catch blocks.
• Use throw and throws effectively in user-defined methods.
• Demonstrate resource cleanup using finally.
Experiments / Tasks
Q1. Write a Java program that demonstrates division by zero without using any exception
handling. Observe and note the run-time error message displayed by the JVM.
Q2. Modify the above program using a try-catch block to handle the ArithmeticException.
Display an appropriate user-friendly message and allow normal program termination.
Q3. Write a program to illustrate handling of multiple exceptions such as ArithmeticException
and ArrayIndexOutOfBoundsException in separate catch blocks.
Q4. Demonstrate an example where a general catch block (for Exception) is placed
before a specific one (for ArithmeticException). Record and explain the compilation
error observed due to unreachable code.
Q5. Write a program that explicitly generates an exception using the throw keyword
and handles it inside a try-catch block.
Q6. Create a method named validateAge(int age) that throws an exception using
the throws clause when age < 18. Handle the exception in the calling method.
Q7. Design a program that demonstrates finally block execution even when:
• an exception occurs,
• no exception occurs, and
• the method contains a return statement inside the try block.
Java Programming Laboratory – Exception Handling
Department of Information Technology [Link] – 3rd Year
Q8. Write a Java program containing nested try-catch-finally blocks to trace how
control transfers between inner and outer blocks.
Q9. Implement a small program to demonstrate exception propagation. Create
three methods where an exception raised in one method propagates up the call stack and
is handled in the main method.
Q10. Draw the hierarchy of Java Exception classes starting from Object →
Throwable → Error / Exception. Label the important built-in subclasses such as IOException,
RuntimeException, NullPointerException, etc.
Q11. Write a program to handle user-defined exceptions. Define a custom exception
class (e.g., InvalidMarksException) and demonstrate its use.
Q12. Develop a Java application that reads input values from the user and gracefully
handles invalid inputs using exception handling.
Q13. Illustrate the difference between checked and unchecked exceptions using
meaningful code examples.
Q14. Prepare a table listing at least ten common built-in exceptions, their packages,
and the conditions under which they occur.
Q15. Discuss the difference between throw and throws keywords with appropriate
code illustrations.
Q16. Write a Java program using FileReader to open a text file that does not exist.
Catch and display the FileNotFoundException using proper messages.
Q17. Write a Java program to demonstrate the use of try-with-resources for
automatic resource management.
Q18. Study and record the output of each of the following cases:
1. Exception occurs and is caught.
2. Exception occurs and is rethrown.
3. Exception does not occur.
4. Exception occurs and no catch block matches.
— End of Lab Manual Sheet —
Java Programming Laboratory – Exception Handling