java重新抛出异常

重抛异常会把异常重抛给上一级环境中的异常处理程序,同一个try块的后续catch子句将被忽略。
如果只是把当前异常对象重新抛出,那么printStackTrace()方法显示的将是原来抛出点的调用栈信息,而非重新抛出点的信息。要想更新这个信息,可以调用fillStackTrace()方法并返回一个Throwable对象,它是通过把当前调用栈信息填入原来那个异常对象建立的,就像这样:

public class Rethrowing {
    //定义f(),g(),g()中调用f()
    public static void f() throws Exception{
        System.out.println("originating the exception in f()");
        throw new Exception("thrown from f()");
    }
    public static void g() throws Exception{
        try {
            f();
        }catch (Exception e){
            System.out.println("Inside g(),e.printStackTrace()");
            e.printStackTrace(System.out);
            throw e;//重新抛出给main的catch
        }
    }
    public static void h() throws Exception{
        try {
            f();
        }catch (Exception e){
            System.out.println("Inside h(),e.printStackTrace()");
            e.printStackTrace(System.out);
            throw (Exception)e.fillInStackTrace();//调用fillStackTrace()重新抛出新的异常
        }
    }
    public static void main(String[] args){
        try {
            g();
        }catch (Exception e){
            System.out.println("main:printStackTrace()");
            e.printStackTrace(System.out);
        }
        try {
            h();
        }catch (Exception e){
            System.out.println("main:printStackTrace()");
            e.printStackTrace(System.out);
        }
    }
}
/*
output:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
    at Exception.Rethrowing.f(Rethrowing.java:9)
    at Exception.Rethrowing.g(Rethrowing.java:13)
    at Exception.Rethrowing.main(Rethrowing.java:31)
main:printStackTrace()
java.lang.Exception: thrown from f()
    at Exception.Rethrowing.f(Rethrowing.java:9)
    at Exception.Rethrowing.g(Rethrowing.java:13)
    at Exception.Rethrowing.main(Rethrowing.java:31)
originating the exception in f()
Inside h(),e.printStackTrace()
java.lang.Exception: thrown from f()
    at Exception.Rethrowing.f(Rethrowing.java:9)
    at Exception.Rethrowing.h(Rethrowing.java:22)
    at Exception.Rethrowing.main(Rethrowing.java:37)
main:printStackTrace()
java.lang.Exception: thrown from f()
    at Exception.Rethrowing.h(Rethrowing.java:26)
    at Exception.Rethrowing.main(Rethrowing.java:37)
 */
Java中,异常是指在程序运行过程中发生的不正常情况,可能会导致程序停止执行。Java提供了异常处理机制来处理这些不正常的事件。抛出异常通常遵循以下步骤: 1. 使用`throw`关键字抛出异常对象。创建一个异常类的实例,然后用`throw`关键字抛出这个实例。 2. 使用`throws`关键字在方法签名中声明可能抛出的异常。这表示调用者需要处理这些异常,或者继续抛出。 异常可以分为两类:受检异常和非受检异常。 - 受检异常(checked exceptions):编译器要求必须处理这些异常,即必须捕获(catch)这些异常或声明抛出它们(throws)。 - 非受检异常(unchecked exceptions):编译器不要求显式处理,包括运行时异常(RuntimeException)及其子类和Error及其子类。 下面是一个简单的例子,展示了如何在Java中抛出一个异常: ```java public class ExceptionExample { public void methodThatThrowsException(int value) throws Exception { if (value < 0) { throw new Exception("参数必须是非负数"); // 创建异常对象并抛出 } // 正常的业务逻辑代码... } public static void main(String[] args) { ExceptionExample example = new ExceptionExample(); try { example.methodThatThrowsException(-1); // 尝试执行可能抛出异常的方法 } catch (Exception e) { System.out.println(e.getMessage()); // 捕获并处理异常 } } } ``` 在上面的代码中,`methodThatThrowsException`方法会抛出一个异常,如果传入的参数是负数。在`main`方法中,我们通过`try-catch`块来捕获并处理这个异常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值