重新抛出异常时出错

时间:2014-04-21 13:19:00

标签: java exception

我在尝试重新抛出异常时收到错误Error:(25, 13) java: unreported exception ExceptionA; must be caught or declared to be thrown

我还是Java的新手,我的书使用下面的“重新抛出”,但是在尝试重新抛出时throw exception;出现错误,即使这是直接基于本书。

import java.io.IOException;

public class catchingExceptions {
    public static void main(String args[]) {
        throwExceptionA();
        throwExceptionB();
        throwNullPointer();
        throwIOException();
    }

    public static void throwExceptionA() {
        try {
            throw new ExceptionA();
        } catch (ExceptionA exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("ExceptionA handled in method throwExceptionA \n");
            throw exception;
        }
    }

    public static void throwExceptionB() {
        try {
            throw new ExceptionB();
        } catch (ExceptionB exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("ExceptionB handled in method throwExceptionB \n");
        }
    }

    public static void throwNullPointer() {
        try {
            throw new NullPointerException();
        } catch (NullPointerException exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("NullPointerException handled in method throwNullPointerException \n");
        }
    }

    public static void throwIOException() {
        try {
            throw new IOException();
        } catch (IOException exception) {
            System.err.printf("Exception: %s \n", exception);
            System.err.println("IOException handled in method throwIOException \n");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

在Java中,任何可以抛出已检查异常的方法都必须声明这一点,因此更改

public static void throwExceptionA()

public static void throwExceptionA() throws ExceptionA

对于未经检查的例外,这不是必需的。

检查或取消选中异常取决于它从哪个类继承。

  • 如果异常从RuntimeException直接或间接(即通过继承链)继承,则视为未选中,并且不需要声明。
  • 所有其他例外情况(即直接或间接从Exception继承,但绝不从RuntimeException继承)被视为已检查,如果可以抛出,则需要声明方法。

考虑到这一点,请考虑以下示例:

public void throwsUndeclaredCheckedException() {
    // compiler error because this exception is not declared (and not caught)
    throw new Exception();
}

public void throwsDeclaredCheckedException() throws Exception {
    // okay, because it was declared
    throw new Exception();
}

public void catchesUndeclaredCheckedException() {
    try {
        throw new Exception();
    } catch( Exception ignored ) {
        // the thrown exception is caught and now ignored, hence the method
        // can't throw and we don't need to declare anything
    }
}

public void throwsUndeclaredRuntimeException() {
    // okay, because it's an unchecked exception
    throw new RuntimeException();
}

public void throwsDeclaredRuntimeException() throws RuntimeException {
    // works, but the "throws" declaration on the method is unnecessary
    throw new RuntimeException();
}

您可以找到有关差异的讨论,例如here

答案 1 :(得分:0)

试试这个:

public static void throwExceptionA() throws ExceptionA
{
    try
    {
        throw new ExceptionA();
    }
    catch ( ExceptionA exception)
    {
        System.err.printf("Exception: %s \n", exception);
        System.err.println ("ExceptionA handled in method throwExceptionA \n" );
        throw exception;
    }
}