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

Discussion in 'ASP .Net' started by Ralph Krausse, Aug 20, 2004.

  1. 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
    Ralph Krausse, Aug 20, 2004
    #1
    1. Advertising

  2. Ralph Krausse

    Shiva Guest

    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" <> wrote in message
    news:...
    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
    Shiva, Aug 20, 2004
    #2
    1. Advertising

  3. Ralph Krausse

    Karl Guest

    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/archive/2004/08/13/214006.aspx

    Karl

    "Ralph Krausse" <> wrote in message
    news:...
    > 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
    Karl, Aug 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    367
    Kevin Spencer
    Aug 7, 2003
  2. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    773
    Chad Myers
    Aug 15, 2003
  3. hansiman

    try catch finally

    hansiman, Jul 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    485
    Sean M
    Jul 14, 2005
  4. Alfredo

    Try Catch and Finally

    Alfredo, Aug 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    1,373
    Kevin Spencer
    Aug 11, 2005
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    794
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page