How to Create an Exception Class in Java

  • google plus

In Brief...

Java has many built-in exception classes, such as NullPointerException and IllegalArgumentException. At times however, you might want to create your own exception class. For example, as opposed to throwing IllegalArgumentException when a 0 is detected as a divisor during a division operation, you might wish to throw a DivideByZeroException. This exception class does not exist in the Java core API, but you can create one yourself. The seven steps below will show you how to create an exception class in Java.

Instructions

  1. First, you will create the custom exception class. Open your text editor and type in the following Java statements:
    Java Source for Create an Exception Class
    The class extends the Exception class that is defined in the Java core API (in the package is java.lang). When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception. The Exception class supports a message property.
  2. Save your file as DivideByZeroException.java.
  3. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Create Exception
  4. Now you will create the program to test your new exception class. Open your text editor and type in the following Java statements:
    Java Source for Testing Exception Class
    Notice that the divideInt method must provide a throw clause because the method potentially throws the DivideByZeroException. To create the exception object, the program uses the throw keyword followed by the instantiation of the exception object. At runtime, the throw clause will terminate execution of the method and pass the exception to the calling method.
  5. Save your file as TestDivideByZeroException.java.
  6. Open a command prompt and navigate to the directory containing your Java program. Then type in the command to compile the source and hit Enter.
    Compile Source for Test Create Exception
  7. Type in the command to run your program and hit Enter.
    Run for Test Create Exception
    The first call to the divideInt method is successful. The second call, using a divisor of 0, causes the DivideByZeroException to be thrown in the divideInt method. The message passed to the constructor is displayed in the output.

Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.

Discuss