重抛异常(re-throw Exception)
我一直以来就是这样写代码:
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.
我一直以来就是这样写代码:
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.