Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Exceptions

Status
Not open for further replies.

komyg

Programmer
Dec 13, 2005
68
BR
Hello, I am new to C#, but I've had some Java experience.

In java you can write a method, throw an exception in it and with a "throws" clause let the method that called it handle the thrown exception, for example:

Code:
public void myMethod1()
{
   try
   {
      myMethod2();
   }
   catch(MyException e)
   {
      // Handle Exception
   }
}

public void myMethod2() throws MyException
{
   throw new MyException("message!");
}

Is there a similar structure in C# to propagate the exception, or do I have to write a try/catch block into every method that the exception must come through and rethrow the exception at each catch block?

For example:

Code:
public void myMethod1()
{
   try
   {
      myMethod2();
   }
   catch(MyException e)
   {
      // Handle exception
   }
}

public void myMethod2() throws MyException
{
   try
   {
      myMethod3();
   }
   catch(MyException e)
   {
      throw new MyException("message!", e);
   }
}

public void myMethod3()
{
   try
   {
      throw new MyException("message!");
   }
   catch(MyException e)
   {
      throw new MyException("message!", e);
   }
}

Thanks,
Komyg
 
you can throw an excpetion anywhere like this
Code:
throw new Exception("foo");
if you want wrap an exception
Code:
try
{
}
catch(Exception e)
{
   throw new MyException("foo", e);
}
or you can process an exception and rethrow
Code:
try
{
}
catch(Exception e)
{
   Log.For(this).Error(e.ToString(), e);
   throw;
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
.net lets the exceptions bubble up by default unlike java if I remember correctly. So you only have to catch it in the calling method. although it can never hurt to log and rethrow like Jason showed you.

Christiaan Baes
Belgium

The future is out there
 
Like chrissie1 says -- in .net, when an exception is thrown, the stack is unrolled until a catch statement is found that can handle it.

If there is no catch statement to catch it, then the program is terminated with a nasty message. But you can add an unhandled-exception handler off the AppDomain object to deal with this. At this point in time, your app is toast. So all you can do is show a dialog to the user with the stacktrace, and ask them to email it to you so you can fix the problem in the next release.

The general rule about catching exceptions is only catch them if there's something you can do about it. Logging generally does not fit this bill, as you can log anywhere -- the stacktrace info will tell you where the problem ocurred, so there's no need to catch & rethrow at the method where it was (maybe) thrown.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top