public class NestedIOException extends IOException implements NestedException, ExceptionDelegateSupport
public URI getURI() throws IOException { URL url = getURL(); try { return UrlUtils.toURI(url); } catch (URISyntaxException ex) { throw new NestedIOException(ex); } }如果外层只捕获 IOException,则内部抛出的其他异常无法捕获,所以必须要将内部其它异常 转换成IOException。 如果直接new IOException(ex)则堆栈重复了,所以使用嵌套异常 new NestedIOException(ex); 另外,也可以在方法内部将IO异常处理了,然后再抛出一个无堆栈信息的IO异常。例如:
public void doPost() throws IOException { OutputStream in = getInput(); try { do something with IOException... } catch (IOException ex) { close(in); logger.error(ex); throw new NestedIOException("post error, please retry..."); } }但是,对于上面这种情况,‘一般’不推荐这么做。建议 使用return false/true 来返回,或者, 如果有返回值,则可以用 return Result
NestedExceptionDelegate
,
序列化表格限定符和类型 | 字段和说明 |
---|---|
static String |
EXCEPTION_PRIFIX |
构造器和说明 |
---|
NestedIOException(Object message,
Object... args) |
NestedIOException(Throwable e) |
NestedIOException(Throwable e,
Object message,
Object... args) |
限定符和类型 | 方法和说明 |
---|---|
Throwable |
getCause()
获取最原始的那个异常对象
|
NestedExceptionDelegate |
getDelegate()
获取 Exception Delegate
|
String |
getMessage() |
IOException |
getOrigException()
重造一个原始的、包含堆栈信息的IOException
|
StackTraceElement[] |
getStackTrace() |
String |
getStackTraceStr() |
void |
printStackTrace() |
void |
printStackTrace(PrintStream err) |
void |
printStackTrace(PrintWriter err) |
String |
toString() |
addSuppressed, fillInStackTrace, getLocalizedMessage, getSuppressed, initCause, setStackTrace
public NestedIOException(Object message, Object... args)
message
- 自定义错误信息args
- 占位符参数--[ 变长参数,用于替换message字符串里面的占位符"{}" ]public NestedIOException(Throwable e)
e
- public String getStackTraceStr()
getStackTraceStr
在接口中 NestedException
public String getMessage()
getMessage
在类中 Throwable
public void printStackTrace()
printStackTrace
在类中 Throwable
public void printStackTrace(PrintWriter err)
printStackTrace
在类中 Throwable
public void printStackTrace(PrintStream err)
printStackTrace
在类中 Throwable
public StackTraceElement[] getStackTrace()
getStackTrace
在类中 Throwable
public IOException getOrigException()
public NestedExceptionDelegate getDelegate()
ExceptionDelegateSupport
getDelegate
在接口中 ExceptionDelegateSupport
Copyright © 2018 jretty-org. All rights reserved.