473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
Nov 18 '05 #1
2 1909
You can order your catches:
try{
}catch (SqlXmlException sqlxmlerr){
}catch (SqlException ex){
}finally{ }

Always go from most specific to least, if you switched those around and did:
try{
}catch (SqlException ex){
}catch (SqlXmlException exsqlxmlerr
}finally{ }

your 2nd catch would happen since your first one would swollow any
exceptions. As for knowing what code throws what exceptions, check the
documentation.

Cyrus from the C# team recently posted about something similar:
http://weblogs.asp.net/cyrusn/archiv...13/214006.aspx

Karl

"Ralph Krausse" <go*******@consiliumsoft.com> wrote in message
news:49**************************@posting.google.c om...
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

Nov 18 '05 #2
Hi,
Your code only catches SqlXmlException, but what is thrown is something
else; the code actually throws IndexOutOfRangeException. For this (& its
derivatives) to be caught, you have to add another catch block hadling this
specific exception:
try
{
}
catch (SqlXmlException sqlXmlErr)
{
}
catch (IndexOutOfRangeException idxErr)
{
}

To catch all exceptions in a single catch block, have a catch for the base
exception System.Exception, which is the root for all exception classes:

try
{
}
catch (Exception xcp)
{
}

Hope this helps.

"Ralph Krausse" <go*******@consiliumsoft.com> wrote in message
news:49**************************@posting.google.c om...
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
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
13
by: Woody Splawn | last post by:
I have a try catch statement in a fucntion that is supposed to return a true or a false My code looks like this: Try mySqlConnection.Open() Dim Da1 As New SqlDataAdapter("Select JnlType,...
2
by: Keith Kowalski | last post by:
I anm opening up a text file reading the lines of the file that refer to a tif image in that file, If the tif image does not exist I need it to send an email stating that the file doesn't exist...
7
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
5
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.