异常包装可能吗?
Exception Wrapping possible?
我有一个异常 'OrekitException',我想将其包装到我自己的异常 class 中。
我该怎么做?
例如在我的一种方法中:
private void configureOrekit() throws OrekitException{
try {
method logic...
} catch (OrekitException oe) {
System.out.println(OrekitException oe);
log.error(oe.getMessage());
}
我自己的例外class:
public class MyException extends Exception {
public MyException ( String message )
{
super( message );
}
}
如何将 OrekitException 合并到 MyException 中,以便在抛出时不在 MyException 中 class?
提供一个也接受异常包装的构造函数:
public class MyException extends Exception {
public MyException (String message, OrekitException exception){
super(message, exception);
}
}
或者为参数使用更广泛的类型来包装任何异常:
public MyException (String message, Exception exception){
super(message, exception);
}
您可以将其用作:
private void configureOrekit() throws MyException{
try {
method logic...
} catch (OrekitException oe) {
throw new MyException("message useful...", oe);
}
请注意,System.out.println()
不是跟踪异常的方法,并且记录异常 和 然后将其包装在您抛出的另一个异常中并不像您那样好仍然可以在任何层记录它,在日志文件中找到一些重复的错误日志会让事情变得不清楚......
一般来说,记录或 throw/propagate 异常:不是两者。
异常允许您指定 cause
。你可以在你的构造函数中获取一个原因,并将它传递给 Exception
的构造函数:
public class MyException extends Exception {
public MyException (String message, Exception cause){
super(message, cause);
}
}
然后您的方法可以抛出 MyException
:
private void configureOrekit() throws MyException{
try {
method logic...
} catch (OrekitException oe) {
System.out.println(OrekitException oe);
log.error(oe.getMessage());
throw new MyException("", oe);
}
}
这允许来自您的 MyException
对象的堆栈跟踪 caused by...
显示原始 OrekitException
的 details/trace
我有一个异常 'OrekitException',我想将其包装到我自己的异常 class 中。
我该怎么做?
例如在我的一种方法中:
private void configureOrekit() throws OrekitException{
try {
method logic...
} catch (OrekitException oe) {
System.out.println(OrekitException oe);
log.error(oe.getMessage());
}
我自己的例外class:
public class MyException extends Exception {
public MyException ( String message )
{
super( message );
}
}
如何将 OrekitException 合并到 MyException 中,以便在抛出时不在 MyException 中 class?
提供一个也接受异常包装的构造函数:
public class MyException extends Exception {
public MyException (String message, OrekitException exception){
super(message, exception);
}
}
或者为参数使用更广泛的类型来包装任何异常:
public MyException (String message, Exception exception){
super(message, exception);
}
您可以将其用作:
private void configureOrekit() throws MyException{
try {
method logic...
} catch (OrekitException oe) {
throw new MyException("message useful...", oe);
}
请注意,System.out.println()
不是跟踪异常的方法,并且记录异常 和 然后将其包装在您抛出的另一个异常中并不像您那样好仍然可以在任何层记录它,在日志文件中找到一些重复的错误日志会让事情变得不清楚......
一般来说,记录或 throw/propagate 异常:不是两者。
异常允许您指定 cause
。你可以在你的构造函数中获取一个原因,并将它传递给 Exception
的构造函数:
public class MyException extends Exception {
public MyException (String message, Exception cause){
super(message, cause);
}
}
然后您的方法可以抛出 MyException
:
private void configureOrekit() throws MyException{
try {
method logic...
} catch (OrekitException oe) {
System.out.println(OrekitException oe);
log.error(oe.getMessage());
throw new MyException("", oe);
}
}
这允许来自您的 MyException
对象的堆栈跟踪 caused by...
显示原始 OrekitException