Exception Handling

G

Guest

I would like to know if this is OK or if it is better to just let the
exception go instead of re-throwing it:

I have:

try
{
}
catch(OracleException ex)
{
// Do something
}
catch(Exception)
{
throw;
}

Should I only have:

try
{
}
catch(OracleException ex)
{
// Do something
}

And let the calling code handle any other type of exception.

Thank you for your input.

JFercan
 
N

Nicholas Paldino [.NET/C# MVP]

Fernando,

I don't see the point of the final throw for Exception, it basically is
the same as not having it there at all. Because of that, I wouldn't use it,
since it just complicates the code.

Hope this helps.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You should have it only if you plan to do something especial in that case.

If you are going to do nothing (as in your code) you do not need it.

Additionally you could use a finally clause that will execute always, you
could close connections, etc there
 
S

sloan

http://blogs.msdn.com/brada/

As per Brad Adams lecture I heard in Raleigh, NC at a user group meeting.
(Go Wolfpack!)

You should have many many , many more

try
{
}
finally
{
}


blocks in your code.. than

try
{}
catch(Exception ex)
{
//throw ex;
}
finally
{}

blocks.

Aka, always put in a finally, but leave the catching to the higher levels.

Most times, I have catching in my business layer, so I can bubble up a user
friendly message to the presentation layer.

While debugging, I'll put the catch in... to find out where the exact
exeptions is happening, but I comment it out for production/released code.

...
 
V

V

Hi Sloan,

this is interesting. i have never worked this way. normally our code
has a catch block most of the time. and the main reason is for logging
of the exception details.

How do you handle the logging of exceptions, then?

- Vaibhav
(interested in learning more and more)
 
O

Otis Mukinfus

Hi Sloan,

this is interesting. i have never worked this way. normally our code
has a catch block most of the time. and the main reason is for logging
of the exception details.

How do you handle the logging of exceptions, then?

- Vaibhav
(interested in learning more and more)

Good point, Vaibhav. I find that logging works best when done at the first
catch. Then you don't have to handle inner exceptions at the bubble up level.

Having said that, I see no problem with either method when not logging.

I too am interested in learning more...
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top