Why doesn't try..catch work on my web page?

Discussion in 'ASP .Net' started by Rob Nicholson, Mar 1, 2005.

  1. Why doesn't try..catch work on my asp.net page?

    Try
    Dim n As Integer = 10
    n = n / 0
    Catch ex As Exception
    ' ignore error
    End Try

    When I single step over the n=n/0 line, it goes straight to the standard
    ASP.NET error page. Why isn't the TRY ignored?

    Thanks, Rob.
     
    Rob Nicholson, Mar 1, 2005
    #1
    1. Advertising

  2. Rob Nicholson

    Curt_C [MVP] Guest

    not of type Exception?

    --
    Curt Christianson
    Site & Scripts: http://www.Darkfalz.com
    Blog: http://blog.Darkfalz.com


    "Rob Nicholson" <> wrote in message
    news:...
    > Why doesn't try..catch work on my asp.net page?
    >
    > Try
    > Dim n As Integer = 10
    > n = n / 0
    > Catch ex As Exception
    > ' ignore error
    > End Try
    >
    > When I single step over the n=n/0 line, it goes straight to the standard
    > ASP.NET error page. Why isn't the TRY ignored?
    >
    > Thanks, Rob.
    >
     
    Curt_C [MVP], Mar 1, 2005
    #2
    1. Advertising

  3. > not of type Exception?

    Sorry don't understand. Same code in a VB client program goes into the Catch
    section.

    Thanks, Rob.
     
    Rob Nicholson, Mar 1, 2005
    #3
  4. > not of type Exception?

    Ahh, yes I do :) I'm building a library function and I've created an
    object/class in that called Exception. Changing it to Catch ex As
    System.Exception worked.

    Hmm, that's going to catch one out especially as VB creates the Catch ex as
    Exception bit when you type Try<CR>. I think I'll rename my own exception
    class.

    Cheers, Rob.
     
    Rob Nicholson, Mar 1, 2005
    #4
  5. Rob Nicholson

    Karl Seguin Guest

    Rob:
    Funny, works for me..the exception is ignored and everything keeps
    working....when I move the division outside the try/catch it craps out as
    expected.

    Perhaps some more context...or an actual full sample...for example, this
    doesn't generate an error for me:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    </html>

    <Script language="vb" runat="server">
    Sub Page_Load
    Try
    Dim n As Integer = 10
    n = n / 0
    Catch ex As Exception
    ' ignore error
    End Try
    End Sub
    </Script>

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "Rob Nicholson" <> wrote in message
    news:...
    > Why doesn't try..catch work on my asp.net page?
    >
    > Try
    > Dim n As Integer = 10
    > n = n / 0
    > Catch ex As Exception
    > ' ignore error
    > End Try
    >
    > When I single step over the n=n/0 line, it goes straight to the standard
    > ASP.NET error page. Why isn't the TRY ignored?
    >
    > Thanks, Rob.
    >
    >
     
    Karl Seguin, Mar 1, 2005
    #5
  6. > Funny, works for me..the exception is ignored and everything keeps
    > working....when I move the division outside the try/catch it craps out as
    > expected.


    Caught by my own cleverness or rather a bit of slack "wizard" functionality
    by VB.NET

    I'd created my own class called Exception so

    Catch ex as Exception

    Was actually:

    Catch ex As Granite.Exception

    Which is why it didn't work...

    I actually think the VB.NET should generate a fully qualified line, i.e.

    Catch ex As System.Exception

    Cheers, Rob.
     
    Rob Nicholson, Mar 1, 2005
    #6
  7. All Exceptions are of type Exception, just as all Objects are of type
    Object.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
    news:%...
    > not of type Exception?
    >
    > --
    > Curt Christianson
    > Site & Scripts: http://www.Darkfalz.com
    > Blog: http://blog.Darkfalz.com
    >
    >
    > "Rob Nicholson" <> wrote in message
    > news:...
    >> Why doesn't try..catch work on my asp.net page?
    >>
    >> Try
    >> Dim n As Integer = 10
    >> n = n / 0
    >> Catch ex As Exception
    >> ' ignore error
    >> End Try
    >>
    >> When I single step over the n=n/0 line, it goes straight to the standard
    >> ASP.NET error page. Why isn't the TRY ignored?
    >>
    >> Thanks, Rob.
    >>

    >
    >
     
    Kevin Spencer, Mar 2, 2005
    #7
  8. I'm not sure what you mean by "it goes to the standard ASP.net error page,"
    but I can tell you why no exception is being caught. None is being thrown.
    In .Net, dividing any number by zero yields Infinity, and does not throw an
    exception.

    If your page is going to an error page, there could be another exceptin
    happening in it somewhere.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Rob Nicholson" <> wrote in message
    news:...
    > Why doesn't try..catch work on my asp.net page?
    >
    > Try
    > Dim n As Integer = 10
    > n = n / 0
    > Catch ex As Exception
    > ' ignore error
    > End Try
    >
    > When I single step over the n=n/0 line, it goes straight to the standard
    > ASP.NET error page. Why isn't the TRY ignored?
    >
    > Thanks, Rob.
    >
     
    Kevin Spencer, Mar 2, 2005
    #8
  9. > In .Net, dividing any number by zero yields Infinity, and does not throw
    > an exception.


    Err, yes it does - it generates an arithmetic overflow.

    Cheers, Rob.
     
    Rob Nicholson, Mar 2, 2005
    #9
  10. > All Exceptions are of type Exception, just as all Objects are of type

    Thats not quite true - all exceptions are of type System.Exception.
    Technically an exception object is one which inherits the
    System.ApplicationException base object. Which is what we're doing - we've
    inherited the exception object as we wanted to store more information with
    it. It works fine through the exception chain.

    Cheers, Rob.
     
    Rob Nicholson, Mar 2, 2005
    #10
  11. Thanks Rob. Your post prompted me to do a little research, and it turns out
    that I was partially right, and you were partially right. An attempt to
    divide an integer or decimal by 0 yields a "DivideByZeroException." However,
    dividing a double or a float by 0 yields Infinity, with no Exception.

    However, in this case, my advice was wrong, as he was attempting to divide
    an integer by 0.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Rob Nicholson" <> wrote in message
    news:...
    >> In .Net, dividing any number by zero yields Infinity, and does not throw
    >> an exception.

    >
    > Err, yes it does - it generates an arithmetic overflow.
    >
    > Cheers, Rob.
    >
     
    Kevin Spencer, Mar 2, 2005
    #11
  12. >> All Exceptions are of type Exception, just as all Objects are of type
    > Thats not quite true - all exceptions are of type System.Exception.


    Picky. Do you always insist on the full NameSpace in order for a statement
    to be correct? You should have quit when you were ahead! ;-)

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Rob Nicholson" <> wrote in message
    news:...
    >> All Exceptions are of type Exception, just as all Objects are of type

    >
    > Thats not quite true - all exceptions are of type System.Exception.
    > Technically an exception object is one which inherits the
    > System.ApplicationException base object. Which is what we're doing - we've
    > inherited the exception object as we wanted to store more information with
    > it. It works fine through the exception chain.
    >
    > Cheers, Rob.
    >
     
    Kevin Spencer, Mar 2, 2005
    #12
  13. > Picky. Do you always insist on the full NameSpace in order for a statement
    > to be correct? You should have quit when you were ahead! ;-)


    In this situation the answer is "Yes". The VB.NET wizard generates the
    following code when you type Try<CR>:

    Try
    Catch ex As Exception
    End Try

    That is dangerous as you might not spot that what you really need is Catch
    ex As System.Exception if you *happen* to have created your own class called
    Exception. Which will take precedence. The net result is that the Catch
    doesn't fire and your code crashes.

    Rob.
     
    Rob Nicholson, Mar 3, 2005
    #13
  14. Rob Nicholson

    Mark Rae Guest

    "Rob Nicholson" <> wrote in message
    news:...

    >> not of type Exception?

    >
    > Ahh, yes I do :) I'm building a library function and I've created an
    > object/class in that called Exception. Changing it to Catch ex As
    > System.Exception worked.
    >
    > Hmm, that's going to catch one out especially as VB creates the Catch ex
    > as Exception bit when you type Try<CR>. I think I'll rename my own
    > exception class.


    Generally, that's why you'll typically see classes with names starting with
    "C" e.g. CException, CRegistry, CDataAccess etc...
     
    Mark Rae, Mar 3, 2005
    #14
  15. > In this situation the answer is "Yes".

    Sloppy logic, Rob. What on earth made you think that I was somehow referring
    to a class that I had created and named "Exception?" What I said was "All
    Exceptions inherit Exception." From the context, it was clear that I was
    referring to the ONLY "Exception" that all Exceptions inherit from. In fact,
    even the original poster mentioned that his class inherits System.Exception.
    It is not possible to create any type of Exception class that does NOT
    inherit System.Exception, which is the base class for ALL Exceptions.

    The VB.NET wizard generates the
    > following code when you type Try<CR>:
    >
    > Try
    > Catch ex As Exception
    > End Try
    >
    > That is dangerous as you might not spot that what you really need is Catch
    > ex As System.Exception if you *happen* to have created your own class
    > called Exception. Which will take precedence.


    Well, darn. I suppose I should switch to VB.Net and let Wizards and
    Designers type all my code for me. I apologize for my ignorance. I always
    write my own try/catch blocks in C#.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Neither a follower nor a lender be.

    "Rob Nicholson" <> wrote in message
    news:%...
    >> Picky. Do you always insist on the full NameSpace in order for a
    >> statement to be correct? You should have quit when you were ahead! ;-)

    >
    > In this situation the answer is "Yes". The VB.NET wizard generates the
    > following code when you type Try<CR>:
    >
    > Try
    > Catch ex As Exception
    > End Try
    >
    > That is dangerous as you might not spot that what you really need is Catch
    > ex As System.Exception if you *happen* to have created your own class
    > called Exception. Which will take precedence. The net result is that the
    > Catch doesn't fire and your code crashes.
    >
    > Rob.
    >
     
    Kevin Spencer, Mar 3, 2005
    #15
  16. Rob Nicholson

    Karl Seguin Guest

    I'm with Kevin on this one...specifying System.Exception because maybe you
    created your own Exception class is overkill....

    Besides, how much code do you write which swollows exceptions?

    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Kevin Spencer" <> wrote in message
    news:OIyug%23$...
    > > In this situation the answer is "Yes".

    >
    > Sloppy logic, Rob. What on earth made you think that I was somehow

    referring
    > to a class that I had created and named "Exception?" What I said was "All
    > Exceptions inherit Exception." From the context, it was clear that I was
    > referring to the ONLY "Exception" that all Exceptions inherit from. In

    fact,
    > even the original poster mentioned that his class inherits

    System.Exception.
    > It is not possible to create any type of Exception class that does NOT
    > inherit System.Exception, which is the base class for ALL Exceptions.
    >
    > The VB.NET wizard generates the
    > > following code when you type Try<CR>:
    > >
    > > Try
    > > Catch ex As Exception
    > > End Try
    > >
    > > That is dangerous as you might not spot that what you really need is

    Catch
    > > ex As System.Exception if you *happen* to have created your own class
    > > called Exception. Which will take precedence.

    >
    > Well, darn. I suppose I should switch to VB.Net and let Wizards and
    > Designers type all my code for me. I apologize for my ignorance. I always
    > write my own try/catch blocks in C#.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > .Net Developer
    > Neither a follower nor a lender be.
    >
    > "Rob Nicholson" <> wrote in message
    > news:%...
    > >> Picky. Do you always insist on the full NameSpace in order for a
    > >> statement to be correct? You should have quit when you were ahead! ;-)

    > >
    > > In this situation the answer is "Yes". The VB.NET wizard generates the
    > > following code when you type Try<CR>:
    > >
    > > Try
    > > Catch ex As Exception
    > > End Try
    > >
    > > That is dangerous as you might not spot that what you really need is

    Catch
    > > ex As System.Exception if you *happen* to have created your own class
    > > called Exception. Which will take precedence. The net result is that the
    > > Catch doesn't fire and your code crashes.
    > >
    > > Rob.
    > >

    >
    >
     
    Karl Seguin, Mar 3, 2005
    #16
  17. > Generally, that's why you'll typically see classes with names starting
    > with "C" e.g. CException, CRegistry, CDataAccess etc...


    Ahh, yes - that's a point of great discussion at the moment in the team. In
    our last large VB6 project, all the classes were prefixed by C but that
    mainly because of earlier projects in C++.

    Trends change :) I thought the C prefix was old fashioned now?

    Rob.
     
    Rob Nicholson, Mar 3, 2005
    #17
  18. > Sloppy logic, Rob. What on earth made you think that I was somehow
    > referring to a class that I had created and named "Exception?" What I said
    > was "All Exceptions inherit Exception." From the context, it was clear
    > that I was


    Sloppy quoting Kevin - if you are going to quote yourself, please do it
    accurately. Changing your quote from "All Exceptions are of type Exception"
    (incorrect) to "All Exceptions inherit Exception" (maybe correct) doesn't
    help your argument.

    Colliding function names have been problem with programming languages for
    many years. Namespaces do resolve them but you can only be 100% guaranteed
    that you uniquely qualify something if you do you the full name.

    My original post was asking "Why is the exception being captured" and the
    answer is because the Catch statement wasn't fully qualified and therefore
    didn't execute as expected.

    Rob.
     
    Rob Nicholson, Mar 3, 2005
    #18
  19. > I'm with Kevin on this one...specifying System.Exception because maybe you
    > created your own Exception class is overkill....


    And why do you think namespaces were invented?

    Rob.
     
    Rob Nicholson, Mar 3, 2005
    #19
  20. > Besides, how much code do you write which swollows exceptions?

    Well one would hope that if you write with code that communications with
    *anything* outside of memory based variables, that you write code that traps
    and therefore swallows exceptions.

    Rob.
     
    Rob Nicholson, Mar 3, 2005
    #20
    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. bienwell
    Replies:
    4
    Views:
    3,898
    bienwell
    May 27, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,220
    Smokey Grindel
    Dec 2, 2006
  3. Adam
    Replies:
    9
    Views:
    609
    red floyd
    Feb 2, 2006
  4. Replies:
    7
    Views:
    1,180
    Lothar Kimmeringer
    Nov 6, 2008
  5. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    615
    Daniel Pitts
    Aug 5, 2009
Loading...

Share This Page