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
You might want to post in one of the .NET NGs rather than the SQL Server XML
one. However...
The exception in your example is not a SqlException - the error is caused by
a call to a managed class (the DataSet class), so it's a System.Exception.
SqlExceptions are thrown when the server raises an exception (so for example
when an error occurs in a stored procedure you have called).
To be safe, you should generally structure your exception handling this way:
try
{
// your code
}
catch (SqlExecption se)
{
// handle SqlException
}
catch (Exception ex)
{
// handle System.Exception
}
finally
{
//clean up code
}
The idea is to create a catch clase for each exception that's likely to
occur, ordering them from more specific to more generic.
Hope that helps,
Graeme
--
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
"Ralph Krausse" <gordingin@.consiliumsoft.com> wrote in message
news:49eb6317.0408200638.7209f631@.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
|||Just to add one tiny point of clarification to Graeme's explanation - the
catch block is only executed if the type of exception specified as the
parameter of the catch clause matches the type of exception that is thrown
by the code in the try block. If you specify System.Exception as the
parameter to the catch block it will catch all exceptions since every
exception in the .Net framework inherits from System.Exception.
In general, catching System.Exception can be dangerous in your code because
it means you may be catching a security exception or a stress condition like
OutOfMemoryException. I agree with Graeme that you want to catch the most
specific exception first and get more general as you go down the catch
blocks but be very careful when catching System.Exception. For more
information on best practices for exception handling check out
http://msdn.microsoft.com/library/de...guidelines.asp
Thanks,
Adam Wiener [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights.
"Graeme Malcolm" <graemem_cm@.hotmail.com> wrote in message
news:O8Ndf$shEHA.4064@.TK2MSFTNGP12.phx.gbl...
> You might want to post in one of the .NET NGs rather than the SQL Server
XML
> one. However...
> The exception in your example is not a SqlException - the error is caused
by
> a call to a managed class (the DataSet class), so it's a System.Exception.
> SqlExceptions are thrown when the server raises an exception (so for
example
> when an error occurs in a stored procedure you have called).
> To be safe, you should generally structure your exception handling this
way:
> try
> {
> // your code
> }
> catch (SqlExecption se)
> {
> // handle SqlException
> }
> catch (Exception ex)
> {
> // handle System.Exception
> }
> finally
> {
> //clean up code
> }
> The idea is to create a catch clase for each exception that's likely to
> occur, ordering them from more specific to more generic.
> Hope that helps,
> Graeme
> --
> --
> Graeme Malcolm
> Principal Technologist
> Content Master Ltd.
> www.contentmaster.com
>
> "Ralph Krausse" <gordingin@.consiliumsoft.com> wrote in message
> news:49eb6317.0408200638.7209f631@.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
>
|||
> 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
User submitted from AEWNET (http://www.aewnet.com/)
|||Well, your code is doing exactly what it says.
You coded:
catch (SqlXmlException sqlxmlerr)
which MEANS catch errors of this type [SqlXmlException] here.
The error thrown was for [System.IndexOutOfRangeException].
To catch ALL errors in on place use something like
catch(Exception e)
you can nest you catch clauses like
catch (SqlXmlException sqlxmlerr) {}
catch(Exception e){}
finianly{}
see BOL and the language specification for more details.
dlr
"Guest" <Guest@.aew_nospam.com> wrote in message
news:%23ZYEUnpSFHA.1160@.tk2msftngp13.phx.gbl...
>
> User submitted from AEWNET (http://www.aewnet.com/)
Monday, February 13, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment