ALL 'try/catch/finally' NOT created equal?

R

Ralph Krausse

I created a try/catch/finally but when an expection is thrown, the
catch does not handle it... (I know this code is wrong, I want to
force the error for this example)


try
{
DataSet ds = new DataSet();
string strID = ds.Tables[0].Rows[0][0].ToString();
}
catch (SqlXmlException sqlxmlerr)
{
sqlxmlerr.ErrorStream.Position = 0;
StreamReader errreader = new StreamReader(sqlxmlerr.ErrorStream);
string err =errreader.ReadToEnd();
errreader.Close();
throw new Exception (err);
}
finally
{
}

This is a ASP.NET application and when the code hits 'string strID =
ds.Tables[0].Rows[0][0].ToString();' the exception is throw and the
result below is displayed in the browser.

*************

Cannot find table 0.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Cannot find table
0.

Source Error:


Line 29: {
Line 30: DataSet ds = new DataSet();
Line 31: strID = ds.Tables[0].Rows[0][0].ToString();
Line 32: }
Line 33: catch (SqlXmlException sqlxmlerr)

*************


Now, if I replace SqlXmlException with System.Exception in the
catch(), the try/catch handles they way I thought and no browser error
happens, the code goes directly to my catch (during debugging, I made
sure).....

So my question.


Just because I have a try/catch doesn't mean it will ALWAYS catch
an exception. I guess I have proven this but wanted confirmation. So
how do I know what exception class to use? Where can I find this
information.

Thanks

Ralph Krausse
www.consiliumsoft.com
Use the START button? Then you need CSFastRunII...
A new kind of application launcher integrated in the taskbar!
ScreenShot - http://www.consiliumsoft.com/ScreenShot.jpg
 
J

Jon Skeet [C# MVP]

Just because I have a try/catch doesn't mean it will ALWAYS catch
an exception.

Absolutely. It only catches the ones you tell it to.
I guess I have proven this but wanted confirmation. So
how do I know what exception class to use? Where can I find this
information.

You look at the documentation, and decide which exceptions (if any) you
want to catch. Often you don't want to catch any until you reach the
top level of your subsystem (or possibly whole application) at which
point you want to catch practically everything. It depends on the
situation though.
 
P

Patrice

You catch only exception of the type you specified (or derived types) in the
catch clause. Here you'll have to add another catch clause as a last resort
to catch System.Exception or derived exception. It needs to be the last one
as .NET will pick the first catch clause that match a particular exception.

Patrice
 
M

Mythran

Ralph Krausse said:
I created a try/catch/finally but when an expection is thrown, the
catch does not handle it... (I know this code is wrong, I want to
force the error for this example)


try
{
DataSet ds = new DataSet();
string strID = ds.Tables[0].Rows[0][0].ToString();
}
catch (SqlXmlException sqlxmlerr)
{
sqlxmlerr.ErrorStream.Position = 0;
StreamReader errreader = new StreamReader(sqlxmlerr.ErrorStream);
string err =errreader.ReadToEnd();
errreader.Close();
throw new Exception (err);
}
finally
{
}

This is an example of how we handle exceptions:

....

Try
' Code that may throw an exception.
Catch Ex As IndexOutOfBoundsException
' This is most likely either a logic exception or the database we accessed
' returned 0 rows but we are still trying to get a row from the dataset.
Catch Ex As MyOwnDatabaseException
' Some sort of database exception that we threw after the database had
' raised an error. Simply for converting the SqlException to a more
' useful exception. An exception like this is derived from
' another custom exception called DatabaseException which is derived
' from ApplicationException.
Catch Ex As Exception
' Dunno what the exception is so we catch it last. Usually, catching
' the Exception in this manner is more or less a critical error that
' happened because we do not know why it was thrown at this point. So,
' we log the exception to the server event log and display a useful
' message to the user and what they can do to continue to use the
' application until it's fixed.
Finally
' Disposing of objects, releasing unmanaged resources, closing
' connections, et cetera.
End Try

NOTE:
When you use a Return statement inside a Try...Catch...block, the code in the
finally still runs. So it's safe to do something like the following:

conn.Open()

Try
Dim ds As DataSet = GetMyDataSet(conn)
Return ds
Catch Ex As Exception
...
Finally
conn.Close()
End Try


Hope this all helps :)

Mythran
 
S

Scott M.

Just because I have a try/catch doesn't mean it will ALWAYS catch
an exception. I guess I have proven this but wanted confirmation. So
how do I know what exception class to use? Where can I find this
information.

Correct.

If you are unsure about what exceptions "might" be thrown, look more
carefully at the exception message that you got when you weren't catching
the right type:

Exception Details: System.IndexOutOfRangeException: Cannot find table 0.

So, we know that you must have an "IndexOutOfRangeException" caught in your
Try...End Try.
 
W

William Ryan eMVP

Or a Try/Catch of it's own. A failure to open a connection will invalidate
whatever else you're trying to do so you may want to give the user some
other options - and it's failure will be fundamentally different from other
failuers.

--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 

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