next up previous
Next: Example of exception handling Up: Unit 09 Previous: How to catch an

The getMessage method

The class Exception, and hence also all its subclasses, have a method getMessage(), which allows one to extract the string associated to the exception.

Example:

try {
  ...
}
catch (NumberFormatException e) {
  System.out.println("Catched NumberFormatException");
  System.out.println(e.getMessage());
}
catch (IOException e) {
  System.out.println("Catched IOException");
  System.out.println(e.getMessage());
}


next up previous
Next: Example of exception handling Up: Unit 09 Previous: How to catch an