Exception与自定义异常

本文通过实例演示了Java中异常处理的方法,包括使用CommException类创建不同类型的异常,并展示了如何通过getMessage(), toString() 和 printStackTrace() 方法来获取和展示异常信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;


/**
 * 
 * 	//只带错误源的Throwable构造函數
 *  public Throwable(Throwable cause) {
 *      fillInStackTrace();
 *      //此时错误消息串为错误根源信息
 *      detailMessage = (cause==null ? null : cause.toString());
 *      this.cause = cause;
 *  }
 *---------------------------------------------------------------------
 * 	Throwable.getMessage():
 * 		//如果未设置错误消息或未设置错误源时返回null
 *  	return detailMessage;
 *  
 * 	Throwable.toString() 源码:
 * 	public String toString() {
 *      String s = getClass().getName();
 *      String message = getLocalizedMessage();
 *      return (message != null) ? (s + ": " + message) : s;
 * 	}
 * 
 * 	Throwable.printStackTrace() 返回格式:toString() + 换行 + 详细堆栈信息
 * 		s.println(this);//调用toString()方法打印错误信息头信息,形如:test.EFinException: 被零除
 *
 * 		//获取详细堆栈信息,信息格式形如:at Test.main(Test.java:8)
 *    	StackTraceElement[] trace = getOurStackTrace();
 *    	for (int i=0; i < trace.length; i++)//循环打印详细堆栈信息
 *      	s.println("\tat " + trace[i]);
 *
 *		//获取错误源
 *    	Throwable ourCause = getCause();
 *    	if (ourCause != null)
 *    	//递归打打印错误根源信息,顺着堆栈往上找,打印的信息形如:Caused by: java.lang.ArithmeticException: / by zero...
 *    	ourCause.printStackTraceAsCause(s, trace);
 * 
 * @author jzj
 *
 */
public class CommException extends RuntimeException
{
	private static final long serialVersionUID = -1654371263422869287L;

	// 错误键,用于前台根据此信息键读取国际化资源,默认值爲null
	private String errKey;

	/**
	 * getMessage()      返回   : null
	 * toString()        返回形如: test.EFinException
	 * printStackTrace() 返回形如:
	 * 	test.EFinException
	 *		at test.TestException.testDefault(TestException.java:21)
	 *		at test.TestException.main(TestException.java:6)
	 */
	public CommException()
	{
		super();
	}

	/**
	 * getMessage()      返回   :  被零除
	 * toString()        返回形如: test.EFinException: 被零除
	 * printStackTrace() 返回形如:
	 * 	test.EFinException: 被零除
	 *		at test.TestException.testSetErrmsg(TestException.java:52)
	 *		at test.TestException.main(TestException.java:7)
	 *
	 * @param message 错误消息串
	 */
	public CommException(String message)
	{
		super(message);
	}

	/**
	 * getMessage()      返回   :  java.lang.ArithmeticException: / by zero
	 * toString()        返回形如: test.EFinException: java.lang.ArithmeticException: / by zero
	 * printStackTrace() 返回形如:
	 * test.EFinException: java.lang.ArithmeticException: / by zero
	 *		at test.TestException.testSetCaused(TestException.java:83)
	 *		at test.TestException.main(TestException.java:8)
	 * Caused by: java.lang.ArithmeticException: / by zero
	 *		at test.TestException.testSetCaused(TestException.java:80)
	 *		... 1 more
	 * 
	 * @param e 错误源
	 */
	public CommException(Throwable e)
	{
		super(e);
	}

	/**
	 * getMessage()      返回   :  被零除
	 * toString()        返回形如: test.EFinException: 被零除
	 * printStackTrace() 返回形如:
	 * 	test.EFinException: 被零除
	 *		at test.TestException.testSetErrmsgCaused(TestException.java:117)
	 *		at test.TestException.main(TestException.java:9)
	 *	Caused by: java.lang.ArithmeticException: / by zero
	 *		at test.TestException.testSetErrmsgCaused(TestException.java:114)
	 *		... 1 more
	 * 
	 * @param message 错误消息串
	 * @param e 错误源
	 */
	public CommException(String message, Throwable e)
	{
		super(message, e);
	}

	/**
	 * 设置错误消息键
	 * 
	 * @param errKey
	 * @return
	 */
	public CommException setErrKey(String errKey)
	{
		this.errKey = errKey;
		return this;
	}

	/**
	 * 返回错误码
	 * 
	 * @return 错误码
	 */
	public String getErrKey()
	{
		return this.errKey;
	}

}

 

package test;

public class TestException {

	public static void main(String[] args) {
		testDefault();
		testSetErrmsg();
		testSetCaused();
		testSetErrmsgCaused();
	}

	/**
	 * 默认构造函数测试
	 */
	public static void testDefault() {
		try {
			System.out.println(1 / 0);
		} catch (Exception e) {
			try {
				throw new CommException();
			} catch (CommException e1) {
				try {
					//null
					System.out.println(e1.getMessage());

					Thread.sleep(10);
					System.out.println("-------------------------");

					//test.CommException
					System.out.println(e1.toString());
					Thread.sleep(10);
					System.out.println("-------------------------");

					/*
					 *test.CommException
					 *	at test.TestException.testDefault(TestException.java:21)
					 *	at test.TestException.main(TestException.java:6)
					 */
					e1.printStackTrace();
					Thread.sleep(10);
					System.out.println("=========================");
				} catch (InterruptedException e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	/**
	 * 只设置错误消息
	 */
	public static void testSetErrmsg() {
		try {
			System.out.println(1 / 0);
		} catch (Exception e) {
			try {
				throw new CommException("被零除");
			} catch (CommException e1) {
				try {
					//被零除
					System.out.println(e1.getMessage());
					Thread.sleep(10);
					System.out.println("-------------------------");

					//test.CommException: 被零除
					System.out.println(e1.toString());
					Thread.sleep(10);
					System.out.println("-------------------------");

					/*
					 *test.CommException: 被零除
					 *	at test.TestException.testSetErrmsg(TestException.java:52)
					 *	at test.TestException.main(TestException.java:7)
					 */
					e1.printStackTrace();
					Thread.sleep(10);
					System.out.println("=========================");
				} catch (InterruptedException e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	/**
	 * 只设置错误源
	 */
	public static void testSetCaused() {
		try {
			System.out.println(1 / 0);
		} catch (Exception e) {
			try {
				throw new CommException(e);
			} catch (CommException e1) {
				try {
					//java.lang.ArithmeticException: / by zero
					System.out.println(e1.getMessage());
					Thread.sleep(10);
					System.out.println("-------------------------");

					//test.CommException: java.lang.ArithmeticException: / by zero
					System.out.println(e1.toString());
					Thread.sleep(10);
					System.out.println("-------------------------");

					/*
					 *test.CommException: java.lang.ArithmeticException: / by zero
					 *	at test.TestException.testSetCaused(TestException.java:83)
					 *	at test.TestException.main(TestException.java:8)
					 *Caused by: java.lang.ArithmeticException: / by zero
					 *	at test.TestException.testSetCaused(TestException.java:80)
					 *	... 1 more
					 */
					e1.printStackTrace();
					Thread.sleep(10);
					System.out.println("=========================");
				} catch (InterruptedException e2) {
					e2.printStackTrace();
				}

			}
		}
	}

	/**
	 * 设置错误消息与错误源
	 */
	public static void testSetErrmsgCaused() {
		try {
			System.out.println(1 / 0);
		} catch (Exception e) {
			try {
				throw new CommException("被零除", e);
			} catch (CommException e1) {
				try {
					//被零除
					System.out.println(e1.getMessage());
					Thread.sleep(10);
					System.out.println("-------------------------");

					//test.CommException: 被零除
					System.out.println(e1);
					Thread.sleep(10);
					System.out.println("-------------------------");

					/*
					 *test.CommException: 被零除
					 *	at test.TestException.testSetErrmsgCaused(TestException.java:117)
					 *	at test.TestException.main(TestException.java:9)
					 *Caused by: java.lang.ArithmeticException: / by zero
					 *	at test.TestException.testSetErrmsgCaused(TestException.java:114)
					 *	... 1 more
					 */
					e1.printStackTrace();
					Thread.sleep(10);
				} catch (InterruptedException e2) {
					e2.printStackTrace();
				}
			}
		}
	}
}

 

运行结果:

 

null
-------------------------
test.CommException
-------------------------
test.CommException
    at test.TestException.testDefault(TestException.java:22)
    at test.TestException.main(TestException.java:8)
=========================
被零除
-------------------------
test.CommException: 被零除
-------------------------
test.CommException: 被零除
    at test.TestException.testSetErrmsg(TestException.java:59)
    at test.TestException.main(TestException.java:9)
=========================
java.lang.ArithmeticException: / by zero
-------------------------
test.CommException: java.lang.ArithmeticException: / by zero
-------------------------
test.CommException: java.lang.ArithmeticException: / by zero
    at test.TestException.testSetCaused(TestException.java:95)
    at test.TestException.main(TestException.java:10)
Caused by: java.lang.ArithmeticException: / by zero
    at test.TestException.testSetCaused(TestException.java:92)
    ... 1 more
=========================
被零除
-------------------------
test.CommException: 被零除
-------------------------
test.CommException: 被零除
    at test.TestException.testSetErrmsgCaused(TestException.java:135)
    at test.TestException.main(TestException.java:11)
Caused by: java.lang.ArithmeticException: / by zero
    at test.TestException.testSetErrmsgCaused(TestException.java:132)
    ... 1 more

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值