展开全部
/**
* class名:NewException
* class说明:在JAVA中,用户程序62616964757a686964616fe58685e5aeb931333365643561如何自定义异常?编程实现一个用户自定义异常
* @author Jr
*
*/
public class NewException extends Exception{
public NewException() {
super();
// TODO Auto-generated constructor stub
}
public NewException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public NewException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public NewException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
/**
* class名:ExceptionTest
* class说明:制造一个异常,来测试NewException类
* @author Jr
*
*/
public class ExceptionTest {
public static void main(String[] args) throws NewException {
System.out.println("开始正常");
try {
String str = null;
str.length();//将抛出异常
System.out.println("结束正常");
} catch (Exception e) {
e.printStackTrace();
throw new NewException("str.length()出现异常", e);
} finally{
}
}
}
自己定义一个异常只需继承(extends)Exception这个类或者Throwable这个类,然后继承父类的构造器就可以了。