473,378 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Throw exception to caller

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);
}

}

}
Nov 15 '05 #1
2 3629
In your catch block put

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

This will create a new exception, with the original exception stored as
innerexception.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode with Nostalgia enabled
Nov 15 '05 #2
You can also re-throw the exception by using the Throw statement without any
parameters. This will preserve the stack trace.

doug

"Mullin Yu" <mu*******@ctil.com> wrote in message
news:uR**************@TK2MSFTNGP11.phx.gbl...
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);
Throw;
}

}

}

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jon Perez | last post by:
I have a C extension function into which I pass a list of lists of tuples: , , , ] I then unpack the values (down to the tuple elements) into their C values using:
16
by: cody | last post by:
I have a method that gets called if the user presses a certain button. If the object is in a state that doesn't allow the calling of that method, what should I do? Should I better throw an...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
2
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
6
by: Phillip Taylor | last post by:
I don't like the way every time I write a bad sql query the debugger takes me to the database access code. It's extremely stable and reliable and the exceptions being thrown are because of SQL...
4
by: Samant.Trupti | last post by:
Hi, bool CControlDrvInstance::IsRebootRequired() const throw() In this code what is throw() do? I just have slight knowledge that is a exception handler but how does it work. If I have this in...
9
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.