简体   繁体   English

为什么异常类型会影响编译的内容,而不会影响编译的内容呢?

[英]Why does exception type affect what will, and won't, compile?

I have two versions of code. 我有两个版本的代码。 In the first version type of exception which is throwed in Method() - NullPointerException , in the second version - Exception . 在第一个版本类型的异常中的哪一个在方法()倒掉- NullPointerException ,在第二版本- Exception However first version will compile but second won't compile. 但是,第一个版本可以编译,而第二个版本则不能编译。 Why is this happen? 为什么会这样呢?

public class Demo
{
    static void Method()
    {
        try
        {
            throw new NullPointerException("error");
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
    public static void main(String argv[])
    { 
        try
        {
            Method();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

This is the second version. 这是第二个版本。

public class Demo
{
    static void Method()
    {
        try
        {
            throw new Exception("error");
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
    public static void main(String argv[])
    { 
        try
        {
            Method();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

stack trace for first version: 第一个版本的堆栈跟踪:

java.lang.NullPointerException: error
    at Demo.Method(Demo.java:7)
    at Demo.main(Demo.java:18)

This is because NullPointerException is a so-called "unchecked" exception. 这是因为NullPointerException是所谓的“未经检查的”异常。 You don't need to declare them in the throws clause. 您无需在throws子句中声明它们。

However, a casual Exception is not unchecked, and you do need to declare in a throws declaration. 但是,偶然的Exception 不会未被选中,并且您确实需要在throws声明中进行声明。 You need to make Method() throw Exception in your second code snippet. 您需要在第二个代码片段中使Method()抛出Exception

Unchecked exceptions are RuntimeException , Error and derivate classes. 未经检查的异常是RuntimeExceptionError和派生类。 NullPointerException derivates from RuntimeException . NullPointerException派生自RuntimeException

NullPointerException is a RunTimeException (or un-checked) and does not have to be handled at compile time. NullPointerExceptionRunTimeException (或未经检查),不必在编译时进行处理。 Exception , however, is considered a checked exception and must be either caught or thrown by any method which encounters it. 但是, Exception被视为检查异常,并且必须由遇到该异常的任何方法捕获或抛出。

The second snippet can be fixed by changing the method declaration as follows: 可以通过如下更改方法声明来修复第二个片段:

static void Method() throws Exception

Unchecked Exceptions - The Controversy 未经检查的例外-争议

Since your method "Method()" throws Exception , you have to declare it on the method signature: 由于您的方法“ Method()”抛出Exception ,因此您必须在方法签名上对其进行声明:

static void Method() throws Exception

And it will compile. 它将编译。 Cheers! 干杯!

Try this as second example: 尝试作为第二个示例:

public class Demo
{
    static void Method() throws Exception
    {
        try
        {
            throw new Exception("error");
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }
    public static void main(String argv[])
    { 
        try
        {
            Method();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

This should compile fine, 这样应该可以编译,

The code of Version 1 does not compile in java version 6 or lower, i guess you are using java 7. 版本1的代码无法在Java版本6或更低版本中编译,我想您正在使用Java 7。

Java 7 looks at compiletime witch exact exception type is beeing thrown. Java 7会在编译时抛出确切的异常类型。 in case 1 it is a runtime exception witch musst not appear in the signatur of the method. 在情况1中,它是运行时异常,没有在方法的签名中显示。

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

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