Throw exception to caller

  • Thread starter Thread starter Mullin Yu
  • Start date Start date
M

Mullin Yu

I know that c# will stack the error if the callee hasn't implemented the try
and catch (the commented code). but, it has the try and catch block(present
running code), the stack won't be passed back to the caller like the
following:

How can I make sure that the error will be passed back to the caller if
having. Do I not implement the try and catch block at those class.

caller
====
private void button3_Click(object sender, System.EventArgs e)
{
Class1 o = new Class1();
try
{
o.division();
}
catch(Exception ex)
{
Error err = new Error();
err.LogFilePath = @"c:\bbb.log";
err.LogError("GEN001", ex.ToString(), 1);
}
}



callee
=====
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}

public void division()
{
/*
int a = 1;
int b = 0;
int i = a/b;
*/

try
{
int a = 1;
int b = 0;
int i = a/b;
}
catch(Exception ex)
{
Error e = new Error();
e.LogFilePath = @"c:\ccc.log";
e.LogError("aaa", ex.ToString(), 1);
}

}

}
 
In your catch block put

throw new Exception("division error", ex);

This will create a new exception, with the original exception stored as
innerexception.
 
You can also re-throw the exception by using the Throw statement without any
parameters. This will preserve the stack trace.

doug
 
Back
Top