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

Re: ALL 'try/catch/finally' NOT created equal? by Jon

Jon
Fri Aug 20 09:51:01 CDT 2004

Ralph Krausse <gordingin@consiliumsoft.com> wrote:

<snip>

> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: ALL 'try/catch/finally' NOT created equal? by Patrice

Patrice
Fri Aug 20 09:54:44 CDT 2004

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

--

"Ralph Krausse" <gordingin@consiliumsoft.com> a écrit dans le message de
news:49eb6317.0408200638.5ed75a85@posting.google.com...
> 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



Re: ALL 'try/catch/finally' NOT created equal? by Mythran

Mythran
Fri Aug 20 10:32:18 CDT 2004


"Ralph Krausse" <gordingin@consiliumsoft.com> wrote in message
news:49eb6317.0408200638.5ed75a85@posting.google.com...
> 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



Re: ALL 'try/catch/finally' NOT created equal? by Scott

Scott
Fri Aug 20 11:08:42 CDT 2004

Your conn.Open() should be in the 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
>
>



Re: ALL 'try/catch/finally' NOT created equal? by Scott

Scott
Fri Aug 20 11:14:15 CDT 2004

> 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.





Re: ALL 'try/catch/finally' NOT created equal? by William

William
Sat Aug 21 13:38:02 CDT 2004

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
"Scott M." <s-mar@nospam.nospam> wrote in message
news:OAzvG$shEHA.2544@TK2MSFTNGP10.phx.gbl...
> Your conn.Open() should be in the 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
> >
> >
>
>