简体   繁体   English

Java中的异常重播错误消息

[英]Exception Recasting Error message in Java

According to this article , new Exception object can be thrown from catch block without specifying "throws" or without enclosing this throw in another try/catch block. 根据本文的介绍 ,可以从catch块抛出新的Exception对象,而无需指定“ throws”,也可以不将此throw封装在另一个try / catch块中。

But when i try to do following in Eclipse Juno: 但是,当我尝试在Eclipse Juno中执行以下操作时:

public class Try {

class Trial extends Exception{}

public static void main(String s[]){
    try{
    }catch(Exception e){
            throw  new Trial();
    }
}

} }

It shows error messages and ask me to add throw or another try/catch around "throw new Trial();". 它显示错误消息,并要求我在“ throw new Trial();”周围添加throw或其他try / catch。 Is it JVM dependent or am i missing something? 是JVM依赖还是我缺少某些东西?

The same article says 同一篇文章说

This is an unchecked exception: we don't need to declare that we throw it, and the caller doesn't need to explicitly handle it. 这是一个未经检查的异常:我们不需要声明我们将其抛出,调用者也不需要显式处理它。 If such an exception occurs, it will be "mopped up" by the uncaught 如果发生此类异常,则未捕获的异常将“清除”

So the exception is unchecked which extends RuntimeException 因此,未检查异常会扩展RuntimeException

You are throwing a checked exception, compiler just complains because your method signature says otherwise 您抛出了一个已检查的异常,编译器只是抱怨,因为您的方法签名表示否则

public static void main(String s[]) throws Trial

should fix it, if you plan to throw it from main. 如果您打算将其从main扔掉,则应修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM