Results 1 to 14 of 14

Thread: [RESOLVED] Error handling Question

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Error handling Question

    Hello,
    When my project goes live I think it would be preferable to redirect users to a custom error page rather than the dot.net page with the stack trace on, how do I implement this with try catch blocks?

    In the forms pages I can use response.redirect but how do I handle this when the error occurs in one of my classes?

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Error handling Question

    This is implementable through the Web.Config file.

    Try googling Web.Config run time error redirecting. Multiple methods exist to get this done and you should find some examples quickly that way.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Error handling Question

    <customErrors defaultRedirect="defaulterrorpage.html"/>

  4. #4

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    But if I put a try catch in will it still show as an error and redirect i.e the only reason I am really want ing to put to finally block in to ensure open data connection are closed if an error occurs?

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Error handling Question

    If you catch an error, it does not bubble up to the error handler (you handled it) unless you re-throw it.

    If you rethrow it, then all code before rethrowing it occurs, all code after does not.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    Thats what I mean, I still need to show an error page when an error occurs but I also need to ensure data connections get closed? How to do both?

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Error handling Question

    Add a Finally to your Try/Catch block and close the connection there.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    Thats what I have and that executes but then because the error has been handled (by the try catch finally) I cannot alert the user that an error occured i.e redirect to a custom errors page after.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Error handling Question

    You can ensure that your connections are closed within the Catch portion. After you've handled the error, do your checks, then rethrown some error (maybe a custom one) so that it redirects.

  10. #10

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    I have this in the classes sup proc

    VB Code:
    1. Catch ex As System.Exception
    2.             If SqlCnnShippersCarriage.State = ConnectionState.Open Then
    3.                 SqlCnnShippersCarriage.Close()
    4.             End If
    5.             Throw New ApplicationException("An Error occured in DespatchNewCarriage")
    6.         End Try

    Then this in the web.config

    VB Code:
    1. <customErrors defaultRedirect="DefaultErrorPage.aspx"/>

    This should work right?

  11. #11

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    Tried testing it with this but its not working it's just showing the standard "Unhandled overflow exception error page when it hits the second throw rether than redirecting to my Customised page?

    VB Code:
    1. AddToSessionInserts()
    2.             Throw New OverflowException
    3.         Catch ex As System.Exception
    4.             If SqlCnnShippersCarriage.State = ConnectionState.Open Then
    5.                 SqlCnnShippersCarriage.Close()
    6.             End If
    7.             Throw New OverflowException
    8.         End Try

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Error handling Question

    Is it?

  13. #13

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    Yep even without the try catch stuff when I throw a completley unhandled exception it isnt using the custom errors redirect.

  14. #14

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Error handling Question

    Fixed thanks all,
    Dumb ass me forgot to set Mode="On"
    Code:
      <customErrors mode="On" defaultRedirect="DefaultErrorPage.aspx"/>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width