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-ex是的,它是一个抽象类,这意味着构造类型为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世界,我发现了关于betweenthrow-ex代码>在C#
与Java
中,那么我可能应该在catch中编写如下内容?感谢e.printStackTrace();投掷e@是的,这是可能的。在较大的应用程序中,日志库用于允许更多日志选项。那么,我可能应该在catch中写以下内容?感谢e.printStackTrace();投掷e@是的,这是可能的。在较大的应用程序中,日志库用于允许更多日志选项。例如。