你会重抛异常(re-throw Exception)不?
来源:互联网 发布:mysql服务器端安装 编辑:程序博客网 时间:2024/05/21 20:29
我一直以来就是这样写代码:
01: public void ThrowFunction()
02: {
03: //Initialize
04: String errMessage = "Just sample throwing.";
05: Boolean isSomeWrong = true;
06: //do something
07: if(isSomeWrong)
08: throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13: Object ret = null; // return value.
14: Object someObj = null; // local value.
15:
16: try
17: {
18: //Initialize
19: ret = new Object();
20: somObj = new Object();
21: // do some thing
22: ThrowFunction();
23: }
24: catch(Exception e)
25: {
26: // Clean up return value.
27: if(ret != null) { ret.Dispose(); ret = null; }
28: throw e;
29: }
30: finally
31: {
32: if(somObj != null) { somObj.Dispose(); somObj = null; }
33: }
34: return ret;
35: }
怎么看都好像没问题,msdn也是这样写的,连Java都是这样写的。但在总是在上一层的
catch中发现少了些信息。最近搜索到一篇文章才发现我正正写错了2年。而且,文章提
到的书也颇有名气。问题在哪?就在于"throw e"。这些写打断了错误栈(stack)链
(break the exception chain or exception stack trace)。如果你查看这个
Exception变量的StackTrace属性:
Exception: System.Exception: "Just sample throwing."
at XXX.GetSomeReturn() in YourProgramPath/SourceCodeName.cs:line 18
是line:28。但是就丢失了关于ThrowFunction发生的事。实际上如果这不是你要的结
果,你只要把28行改为'throw;'即可。如果你catch exception仅仅为了clean-up。
还可以把24行改为'catch(Exception)' 甚至'catch'。最懒惰的写法就是:
01: public void ThrowFunction()
02: {
03: //Initialize
04: String errMessage = "Just sample throwing.";
05: Boolean isSomeWrong = true;
06: //do something
07: if(isSomeWrong)
08: throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13: Object ret = null; // return value.
14: Object someObj = null; // local value.
15:
16: try
17: {
18: //Initialize
19: ret = new Object();
20: somObj = new Object();
21: // do some thing
22: ThrowFunction();
23: }
24: catch
25: {
26: // Clean up return value.
27: if(ret != null) { ret.Dispose(); ret = null; }
28: throw;
29: }
30: finally
31: {
32: if(somObj != null) { somObj.Dispose(); somObj = null; }
33: }
34: return ret;
35: }
当然如果你想重新包装你的异常,则:
01: public void ThrowFunction()
02: {
03: //Initialize
04: String errMessage = "Just sample throwing.";
05: Boolean isSomeWrong = true;
06: //do something
07: if(isSomeWrong)
08: throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13: Object ret = null; // return value.
14: Object someObj = null; // local value.
15:
16: try
17: {
18: //Initialize
19: ret = new Object();
20: somObj = new Object();
21: // do some thing
22: ThrowFunction();
23: }
24: catch(Exception e)
25: {
26: // Clean up return value.
27: if(ret != null) { ret.Dispose(); ret = null; }
28: throw new YourException("Your costum error message",e);
29: }
30: finally
31: {
32: if(somObj != null) { somObj.Dispose(); somObj = null; }
33: }
34: return ret;
35: }
上面的写法:
throw new YourException("Your costum error message",e);
同样不会打断stack trace。比如需要Log下异常然后重抛该异常而不打断stack trace。
可以这样写:
01: public void ThrowFunction()
02: {
03: //Initialize
04: String errMessage = "Just sample throwing.";
05: Boolean isSomeWrong = true;
06: //do something
07: if(isSomeWrong)
08: throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13: Object ret = null; // return value.
14: Object someObj = null; // local value.
15:
16: try
17: {
18: //Initialize
19: ret = new Object();
20: somObj = new Object();
21: // do some thing
22: ThrowFunction();
23: }
24: catch(Exception e)
25: {
26: // Clean up return value.
27: if(ret != null) { ret.Dispose(); ret = null; }
28: YourLogClass.LogException(e);
29: throw;
30: }
31: finally
32: {
33: if(somObj != null) { somObj.Dispose(); somObj = null; }
34: }
35: return ret;
25: }
Link
Read the original article about re-throw exception.
- 你会重抛异常(re-throw Exception)不?
- C++ 异常 exception throw
- java中的throw new Exception();和throw e;异常抛出
- JAVA 异常 throwable exception error throws throw
- throw new Exception抛出异常的接收
- [Java] throw Exception 声名抛出异常
- JAVA 异常throwable exception error throws throw
- JAVA 异常 throwable exception error throws throw
- php throw new Exception php异常处理
- 异常throw new Exception相关代码
- JAVA 异常 throwable exception error throws throw
- JAVA 异常 throwable exception error throws throw
- 异常 Exception RuntimeException throws throw 自定义异常 finally
- throw Exception
- Java异常--其他概念—throw、throws、Exception、RuntimeException、断言
- java中的异常(exception)&&try,catch,finally throw&&throws
- spring事务不回滚throw的Exception异常的解决方法
- java异常-Error和Exception;try,catch,throw,throws,finally
- 张望“头文件与内联函数”
- POJ2342Anniversary party题解动态规划DP
- datalist 分页
- 【小阅读^大脑袋】0823 NO.404
- dfrfrrerere
- 你会重抛异常(re-throw Exception)不?
- 2010.8.23
- iuoooo
- [随笔]明年,你还爱我么..(网络转载)
- Eclipse Galileo 简介
- VS2005+SQL SERVER 2000+AspNetPager1 分页存储
- ACM试题的一些讲解
- UDT协议-基于UDP的可靠数据传输协议
- Android高手进阶教程(八)之----Android Widget开发案例(世界杯倒计时!)