Structured error handling practises in asp.net
-
Hi, I have a query regarding what would be a 'best practice' for SEH in an asp.net web app. At present I have Object as my return type from class functions. Within these functions use a try...catch block and return an error object if there's an error or the correct type, say datatable if the process completes as expected. The calling sub then evaluates the returned type and acts accordingly. While this works ok for me, it is a bit long winded and I can't help feeling I am missing the point somewhere. What do you guys do? Thanks if anyone has any tips. Shaun If I had all the money I'd spent on drink, I'd spend it on drink - Sir Henry at Rawlinson's End
-
Hi, I have a query regarding what would be a 'best practice' for SEH in an asp.net web app. At present I have Object as my return type from class functions. Within these functions use a try...catch block and return an error object if there's an error or the correct type, say datatable if the process completes as expected. The calling sub then evaluates the returned type and acts accordingly. While this works ok for me, it is a bit long winded and I can't help feeling I am missing the point somewhere. What do you guys do? Thanks if anyone has any tips. Shaun If I had all the money I'd spent on drink, I'd spend it on drink - Sir Henry at Rawlinson's End
Exceptions are special in that when one is thrown, the .NET runtime will go up the function call-stack until it finds a catch block that can handle the exception. What this means is that you can put a try-catch at the "root" function (the one that calls all the other functions) and any exceptions thrown by any lower functions will be caught in the root function's catch block. Here's an example:
Sub Main()
Try
DoStuff1()
Catch ex As Exception
' Handle exception
End Try
End SubPrivate Sub DoStuff1()
DoStuff2()
End SubPrivate Sub DoStuff2()
DoStuff3()
End SubPrivate Sub DoStuff3()
' Generate an exception
End SubIf DoStuff3 generates an exception, .NET will jump to the catch block in Main. This way you can assume that all the values returned from your functions will be correct since if there was an error in the function, .NET would jump to the outer catch block, skipping any code that used the return value.
-
Exceptions are special in that when one is thrown, the .NET runtime will go up the function call-stack until it finds a catch block that can handle the exception. What this means is that you can put a try-catch at the "root" function (the one that calls all the other functions) and any exceptions thrown by any lower functions will be caught in the root function's catch block. Here's an example:
Sub Main()
Try
DoStuff1()
Catch ex As Exception
' Handle exception
End Try
End SubPrivate Sub DoStuff1()
DoStuff2()
End SubPrivate Sub DoStuff2()
DoStuff3()
End SubPrivate Sub DoStuff3()
' Generate an exception
End SubIf DoStuff3 generates an exception, .NET will jump to the catch block in Main. This way you can assume that all the values returned from your functions will be correct since if there was an error in the function, .NET would jump to the outer catch block, skipping any code that used the return value.