Re-throw Exception)
I always write code like this:
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 :}
It seems that there is no problem in reading it. msdn also writes it like this, and even Java writes it like this. But at the previous layer
Catch found that some information is missing. I recently searched an article and found that I was wrong for two years. In addition, the article mentions
He is also famous for his book. Where is the problem? Lies in "throw e ". These writes interrupt the stack chain.
(Break the exception chain or exception stack trace ). If you view this
StackTrace attribute of Exception variable:
Exception: System. Exception: "Just sample throwing ."
At XXX. GetSomeReturn () in YourProgramPath \ SourceCodeName. cs: line 18
Is line: 28. However, the ThrowFunction is lost. In fact, if this is not the end
If yes, you only need to change the 28 rows to 'Throw. If you catch exception only for clean-up.
You can also change the 24 rows to 'catch (Exception) 'or 'catch '. The lazy statement is:
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 :}
Of course, if you want to repackage your exceptions, then:
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 :}
The preceding statement is as follows:
Throw new YourException ("Your costum error message", e );
The stack trace will not be interrupted. For example, You need to Log an exception and then re-Throw the exception without interrupting the stack trace.
You can write as follows:
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.