Handling Exceptions Labs
The purpose of this lab is to handle or declare exceptions, and call methods which every exception has.
com.example.exceptions.labs.ExceptionsLab
- Run ExceptionsLab.java and carefully examine the output, finding where each line of output was produced. Now, in
method1, uncomment the call tomethod2. Does the code compile?
Change the definition of method1 so that it declares that it throws Exception. Is this enough to compile the class?
Add throws Exception declarations to methods until you can compile an run ExceptionsLab.java.
(Solution: com.example.exceptions.solutions.ExceptionsLab)
- In
run, put the call tomethod1in atryblock. In thecatchblock, print a message saying that the exception was caught.
Which methods can now remove their throws declarations?
Which methods now complete normally?
(Solution: com.example.exceptions.solutions.ExceptionsLab2)
- In the
catchblock, add statements to print: - The exception's message.
- The exception object as a
String. - The exception's stack trace.
(Solution: com.example.exceptions.solutions.ExceptionsLab3)