Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception handling 如何在java中抛出SystemException?_Exception Handling_Try Catch_Java 7_Throw - Fatal编程技术网

Exception handling 如何在java中抛出SystemException?

Exception handling 如何在java中抛出SystemException?,exception-handling,try-catch,java-7,throw,Exception Handling,Try Catch,Java 7,Throw,我看到很多人这样写道: try { //something } catch (IOException e){ throw new SystemException("IO Error", e); } 我得到了“无法实例化类型SystemException”错误,并且SystemException似乎是一个抽象类,我如何能够抛出它 是的,它是一个抽象类,这意味着构造类型为SystemException的对象没有意义。建议使用更有意义的异常类型 您在代码中提到了IOExcept

我看到很多人这样写道:

try {
       //something
}
catch (IOException e){
    throw new SystemException("IO Error", e);
}

我得到了
“无法实例化类型SystemException”
错误,并且SystemException似乎是一个抽象类,我如何能够抛出它

是的,它是一个抽象类,这意味着构造类型为
SystemException
的对象没有意义。建议使用更有意义的异常类型

您在代码中提到了
IOException
。这意味着与I/O操作相关的异常,捕手可以相应地采取行动(可能是特殊的日志级别、一些I/O清理等)

在您的特殊情况下,我认为您应该将其更改为:

try {
    //something
}
catch (IOException e) {
    // log exception info and other context information here
    // e.g. e.printStackTrace(); 

    // just rethrowing the exception (call stack is still there)
    throw e;
}

p.S.相当离题,但来自.NET世界,我发现了关于between
throw-exSystemException
的对象没有意义。建议使用更有意义的异常类型

您在代码中提到了
IOException
。这意味着与I/O操作相关的异常,捕手可以相应地采取行动(可能是特殊的日志级别、一些I/O清理等)

在您的特殊情况下,我认为您应该将其更改为:

try {
    //something
}
catch (IOException e) {
    // log exception info and other context information here
    // e.g. e.printStackTrace(); 

    // just rethrowing the exception (call stack is still there)
    throw e;
}

p.S.相当离题,但来自.NET世界,我发现了关于between
throw-ex
C#
Java

中,那么我可能应该在catch中编写如下内容?感谢e.printStackTrace();投掷e@是的,这是可能的。在较大的应用程序中,日志库用于允许更多日志选项。那么,我可能应该在catch中写以下内容?感谢e.printStackTrace();投掷e@是的,这是可能的。在较大的应用程序中,日志库用于允许更多日志选项。例如。