Java面向对象设计 – Java自定义异常
我们可以创建我们自己的异常类。
它们必须扩展现有的异常类。
<Class Modifiers> class <Class Name> extends <Exception Class Name> {
}
<Class Name> is the exception class name.
创建一个MyException类,它扩展了java.lang.Exception类。
语法
语法如下:
public class MyException extends Exception {
}
异常类与Java中的任何其他类一样。通常,我们不向我们的异常类中添加任何方法。
许多有用的方法可以用来查询异常对象的状态在Throwable类中声明。
自定义异常类构造函数
通常,我们为我们的异常类包括四个构造函数。
所有构造函数将使用super关键字调用其超类的相应构造函数。
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);
}
}
第一个构造函数创建一个具有null的异常作为其详细消息。
第二个构造函数创建一个具有详细消息的异常。
第三和第四个构造函数允许您通过包装/不包含详细消息的另一个异常来创建异常。
您可以抛出类型MyException的异常。
throw new MyException("Your message goes here");
我们可以在方法/构造函数声明中的throws子句中使用MyException类,或者在catch块中使用参数类型。
public void m1() throws MyException {
}
或捕获异常类
try {
}catch(MyException e) {
}
Throwable
下面的列表显示了Throwable类的一些常用方法。
Throwable类是Java中所有异常类的超类。此表中显示的所有方法在所有异常类中都可用。
- Throwable getCause()
返回异常的原因。如果未设置异常的原因,则返回null。 - String getMessage()
返回异常的详细消息。 - StackTraceElement[] getStackTrace()
返回堆栈跟踪元素的数组。 - Throwable initCause(Throwable cause)
设置异常的原因。
有两种方法可以将异常设置为异常的原因。其他方法是使用构造函数,它接受原因作为参数。 - void printStackTrace()
在标准错误流上打印堆栈跟踪。 - void printStackTrace(PrintStream s)
将堆栈跟踪打印到指定的PrintStream对象。 - void printStackTrace(PrintWriter s)
将堆栈跟踪打印到指定的PrintWriter对象。 - String toString()
返回异常对象的简短描述。
例外
以下代码演示了对异常类使用printStackTrace()方法。
public class Main {
public static void main(String[] args) {
try {
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);
}
}
上面的代码生成以下结果。
例2
以下代码显示了如何将异常的堆栈跟踪写入字符串。
import java.io.PrintWriter;
import java.io.StringWriter;
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);
}
}
上面的代码生成以下结果。


国外主机测评 - 国外VPS,国外服务器,国外云服务器,测评及优惠码













