next up previous
Next: The throw statement Up: Unit 10 Previous: Checked exceptions and runtime

Definition of new exceptions

A new exception can be defined starting from the class Exception or from one of its descendants.

public class MyException extends Exception {
  public MyException (String message) {
    super(message);
  }
}

The class MyException specifies a particular message to visualize. The constructor of an Exception takes as parameter a String that is printed when the exception occurs.

It is also possible to define an exception class as a subclass of RuntimeException, instead of Exception. In this way, it will not be necessary to handle explicitly such exceptions, since they are of type unchecked.


next up previous
Next: The throw statement Up: Unit 10 Previous: Checked exceptions and runtime