1. Learn
  2. /
  3. Courses
  4. /
  5. Data Types and Exceptions in Java

Connected

Exercise

Exception object

It is not unusual for a piece of code to throw a number of exceptions. One way to handle this situation is to try-catch all exceptions and then use the exception object to determine what issue has happened and to output information from the Exception to debug the problem.

Here, you will modify the SplitTheBill application again to catch a generic Exception and use data from the Exception object to understand the cause of the problem. Exception objects contain a lot of useful information, including a trace of methods leading to the problem.

Instructions

100 XP
  • In the catch block, capture any Exception using catch (Exception e).
  • Use the Exception and object reference (e) and the getClass() method in the catch block to display the type (class) of exception caught.
  • Use the Exception object reference (e) and the getMessage() method to print the exception's message.
  • Print the "stack trace" to the output stream using the Exception object reference (e) and the printStackTrace() method.