重抛异常(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.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?