467,877 Members | 1,141 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

try catch just does not work

I'm writing an app on the PDA using C# with .net 1.1. It is all working well
except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception
way up the stack at a Form.ShowDialog(). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw new Exception(ex.Message);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
Jun 8 '06 #1
  • viewed: 3542
Share:
3 Replies
This may not answer your problem directly, but what is the purpose of your
try/catch statement? What is happening (if it did work) is that your
catching an exception, creating a new instace of the exception and throwing
it. This will erase your stack trace which will track down the root of where
the orginal exception gets thrown. If all you need to do is bubble it up to
the calling method, take out the try/catch all together, that way you
preserve the stack trace in your exception.

Or if you want to personalize your error messages you can do something like
this:

try
{
_Command.ExecuteNonQuery();
}
catch(SqlException ex)
{
string errMsg = "SQL Error received: " + ex.Message
throw new SqlException(errMsg, ex); //passing an inner exception
parameter preserves your stack trace in the exception.
}
catch(Exception ex)
{
string errMsg = "Unexpected error occured while executing non query:
" +
ex.Message
throw new Exception(errMsg, ex);
}

Personally, I would leave out the try/catch statement.

"Michael C" wrote:
I'm writing an app on the PDA using C# with .net 1.1. It is all working well
except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception
way up the stack at a Form.ShowDialog(). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw new Exception(ex.Message);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}

Jun 8 '06 #2

If you research "Brad Abrams" and try catch finally, you'll find a blog.

Basically, a good rule of thumb is that you should have alot more
try/finally
statements

instead of
try/catch
or
try/catch/finally
statements.

try
{
//do something
}
finally
{
//close up resources like IDataReader for instance
}
Then somewhere, maybe the presentation layer....... you have the catch
statement.

Its actually wasted effort to catch and an an exception, and then rethrow
it.

...

The previous poster is also correct. You don't throw a new Exception.
Well, perhaps you want a specific user friendly message to display. Then
you ~should use the inner exception mechanism the other poster talks about.

If you did this:

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

that would be closer to what you want I think.... However, you need to look
at the first part of my post, which talks about only a try/finally

....................................


"Michael C" <no****@nospam.com> wrote in message
news:Om**************@TK2MSFTNGP02.phx.gbl...
I'm writing an app on the PDA using C# with .net 1.1. It is all working well except in some cases a try catch is simply ignored and a totally different
error is returned. I've got code like below to execute a command. I'm
simulating loss of connection by stopping sqlserver during a transaction.
The problem is I cannot trap the error and I get a stack overflow exception way up the stack at a Form.ShowDialog(). I've done a break on
executenonquery below and then pushed F11 and the catches both get ignored
and execution jumps up the stack without any code running at all.

I know this is a bit of an obsure problem but I appreciate any input. I
couldn't find anything on google.
Regards,
Michael

try
{
_Command.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw new Exception(ex.Message);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}

Jun 8 '06 #3
"sloan" <sl***@ipass.net> wrote in message
news:uh**************@TK2MSFTNGP03.phx.gbl...
Its actually wasted effort to catch and an an exception, and then rethrow
it.


That's true but this is only sample code. This statement had no try catch at
all and the catch was higher up in the call stack. I added these catches
just as a test giving me somewhere to add a breakpoint while keeping the
catch statements higher up working. I thought having the catch directly
around the statement and throwing it again might eliminate the problem but
unfortunately it didn't.

Michael
Jun 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Adrian Stutz [anime_layer] | last post: by
2 posts views Thread by Eclectic | last post: by
4 posts views Thread by Das | last post: by
11 posts views Thread by Jim | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.