019-007-003

Creating Exception Classes: Defining Custom Exceptions

Easy

Problem Description

In this problem, you will create a program that extends the Exception class to implement a custom InvalidAgeException, throws it during age validation, and catches it to display the result to standard output.

Custom Exception Classes

Create custom exceptions by extending the Exception class.

Basic Custom Exception

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

Learning Points

  • Extend Exception to create custom exceptions
  • Pass message to parent class with super(message)
  • Can express application-specific errors

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up