We can create our own exception classes.
They must extend an existing exception class.
<Class Modifiers> class <Class Name> extends <Exception Class Name> { }
<Class Name> is the exception class name.
To create a MyException class, which extends the java.lang.Exception class.
The syntax would be as follows:
public class MyException extends Exception { }
An exception class is like any other classes in Java. Typically, we do not add any methods to our exception class.
Many useful methods that can be used to query an exception object's state are declared in the Throwable class.
Typically, we include four constructors to our exception class.
All constructors will call the corresponding constructor of its superclass using the super keyword.
class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(Throwable cause) { super(cause); } }
The first constructor creates an exception with null as its detailed message.
The second constructor creates an exception with a detailed message.
The third and fourth constructors let you create an exception by wrapping another exception with/without a detailed message.
You can throw an exception of type MyException.
throw new MyException("Your message goes here");
We can use the MyException class in a throws clause in a method/constructor declaration or as a parameter type in a catch block.
public void m1() throws MyException {
}
or catch MyException class
try { }catch(MyException e) { }
The following list shows some of the commonly used methods of the Throwable class.
Throwable class is the superclass of all exception classes in Java. All of the methods shown in this table are available in all exception classes.
The following code demonstrates the use of the printStackTrace() method for an exception class.
public class Main { public static void main(String[] args) { try {//ww w . j a v a 2s. c o m m1(); } catch (MyException e) { e.printStackTrace(); // Print the stack trace } } public static void m1() throws MyException { m2(); } public static void m2() throws MyException { throw new MyException("Some error has occurred."); } } class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(Throwable cause) { super(cause); } }
The code above generates the following result.
The following code shows how to write Stack Trace of an Exception to a String
import java.io.PrintWriter; import java.io.StringWriter; /*w w w .ja v a2s .co m*/ public class Main { public static void main(String[] args) { try { m1(); } catch (MyException e) { String str = getStackTrace(e); System.out.println(str); } } public static void m1() throws MyException { m2(); } public static void m2() throws MyException { throw new MyException("Some error has occurred."); } public static String getStackTrace(Throwable e) { StringWriter strWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(strWriter); e.printStackTrace(printWriter); // Get the stack trace as a string String str = strWriter.toString(); return str; } } class MyException extends Exception { public MyException() { super(); } public MyException(String message) { super(message); } public MyException(String message, Throwable cause) { super(message, cause); } public MyException(Throwable cause) { super(cause); } }
The code above generates the following result.