ASP.NET Error Handling

Posted October 28, 2002

Chat Date: October 10, 2002

Chat Participants:

  • Doug Seven, Microsoft ASP.NET MVP
  • Donny Mack, Co-Founder of DotNetJunkies
  • Thomas Johansen, Freelance developer
  • John Perry, Community Program Manager

Host Guest_jperry_ms
Welcome to today’s ASP.NET chat on Error Handling. I will ask the Hosts to introduce themselves.

Host Guest_Doug_Seven_MVP
I'm Doug Seven, Microsoft ASP.NET MVP and co-founder of DotNetJunkies.com...Let's get it on!

Host Guest_Aylar
I'm a young programmer from Norway. I have been programming since the age of 12, when I did some pretty straightforward Basic programming. At the age of 15 I discovered other programming languages such as Python, C, C++, and more.

Host Guest_Donny_Mack_MVP
Hello All, Donny Mack here. Short Bio: Co-Founder of DotNetJunkies which operates DotNetJunkies.com, does software development, and training centered around .NET. I've written a few books and probably hundreds of articles on .net

Host Guest_Aylar
I'm currently developing .Net based solutions for companies as a freelancer. I also write articles for several sites online (some articles can be found in my column at ASP Alliance)

Host Guest_Donny_Mack_MVP
Thanks for having me! For those interested in user groups I'm also on the NYC .NET Developers Group advisory board so please come and check out our meetings.

Host Guest_jperry_ms
And I'm John Perry, Community Program Manager for ASP.Net

Host Guest_jperry_ms
The Input Room is where you can enter questions for our Hosts today. We will read them and select questions to answer. The questions and answers will be posted in the Reading Room.

Host Guest_jperry_ms
Let's get started! Fire away with your questions for our Hosts.

Host Guest_jperry_ms
Q: BWaide: Q :I've read something about not to inherit from System.Exception but System.ApplicationException. Why?

Host Guest_Doug_Seven_MVP
A: System.Exception can be any type of exception, such as a SqlException, ApplicationException, SystemException etc.

Host Guest_Doug_Seven_MVP
A: In your class you typically will want to handle only ApplicationExceptions with a derived class. This doesn't mean you can only derive from ApplicationException - its just the recommended method.

Host Guest_jperry_ms
Q: I want to know how I can move back to try block from catch block

Host Guest_Donny_Mack_MVP
Q: The only way is to re-invoke the method again. Although, after in the catch block you can either try to recover there or execute some stuff in the finally block.

Host Guest_jperry_ms
Q: alex: Sometimes the breakpoints I set in VS.NET are ignored. In debug mode there's a question mark on the breakpoint and says "no executable code is associated with this line". Why do I get this problem? Breakpoints work fine in other files in the

Host Guest_Doug_Seven_MVP
A: Breakpoints will only work on executable lines of code. For example, a break point will not work on a comment line, or on Dim x As String. It will work on Dim x As String = "Something" because this is an executable line.

Host Guest_jperry_ms
Q: LugNut: What is the recommended way to notify a user that an exception has been thrown (i.e. windows apps use a message box so what should web apps use)?

Host Guest_Aylar
A: Well there are many ways to do this, it mostly comes down to preference. But on pages I know will usually have a fairly big number of exceptions (Like a page with e-mail functionality) I tend to add a label control and add either the error message directly from the exception, or some description of what occurred. On general exceptions just Response.Write() the exception message.

Host Guest_jperry_ms
Q: sm :like in vb 6 we do resume next, it there something .net like resume next

Host Guest_Doug_Seven_MVP
Q: On Error Resume Next is still available, but structured error handling is the preferred method now. In a Try/Catch block you catch the error and provide alternate processing instruction.

Host Guest_Doug_Seven_MVP
A: It is not uncommon in my code to have several Try/Catch blocks in a row to handle a variety of potential errors.

Host Guest_jperry_ms
Q: sm: i'm doing some processing in try block and some error occurs I check the error and it is not the error to stop the execution and want to continue

Host Guest_Donny_Mack_MVP
A: You have two choices. You can either filter the exception handlers by error number and do what you need to do in that catch block. Or the better yet prevent the exception before it happens.

Host Guest_Donny_Mack_MVP
A: If you know what might cause a particular exeception validate the object first. For instance, if you know you might get a divide by zero exception check if the value you are dividing by is zero before doing the equation

Host Guest_Donny_Mack_MVP
A: There is a lot less overhead by doing option two also

Host Guest_jperry_ms
Q: dlussier:: What would you suggest to be the best method for handling a constraint exception from a sql database within an asp.net web form?

Host Guest_Doug_Seven_MVP
A: The standard format for handling something like this is to have a Try/Catch like so:

Host Guest_Doug_Seven_MVP
A: Try

Host Guest_Doug_Seven_MVP
A: 'Do the database stuff

Host Guest_Doug_Seven_MVP
A: Catch sqlex as SqlException

Host Guest_Doug_Seven_MVP
A: 'Do specific Sql exception handling

Host Guest_Doug_Seven_MVP
A: Catch ex As Exception

Host Guest_Doug_Seven_MVP
A: 'Do general exception handling

Host Guest_Doug_Seven_MVP
A: End Try

Host Guest_Doug_Seven_MVP
A: dlussier: </end>

Host Guest_jperry_ms
Q: plitwin: I like Try..Catch error handling but I don't get why it's called "structured" error handling and On Error Goto X is called "unstructured". Anybody can explain that to me?

Host Guest_Donny_Mack_MVP
A: Stuctured is just a way to describe it. I believe it's mostly due to the fact that "Structured Error Handling" using a structure similar to a SELECT CASE

Host Guest_Donny_Mack_MVP
A: Also, execution can be resumed with unstructured while with structured execution stops within the block and goes to the next.

Host Guest_Donny_Mack_MVP
A: Next being the catch block or finally block

Host Guest_jperry_ms
Q: BWaide: Is there a way to find out, what exceptions a thrown from a method? Sometimes the framework doc doesn't tell us? Can I find out with reflection or something like this?

Host Guest_Doug_Seven_MVP
A: You can write the Exception.Message, Exception.Source, and Exception.StackTrace to a log file (either an event log or a text file). This will provide the information you need to trace the exceptions later.

Host Guest_jperry_ms
Q: carl: I continue to get the default error page showing up even though I have written an Application_Error procedure in the global.asax.vb file. What's wrong? Why won't it work?

Host Guest_jperry_ms
Q: sm: russ: if u check docum. isnothing is fucntion

Host Guest_Doug_Seven_MVP
A: You have to import the Microsoft.VisualBasic class library. You will get the same results with If x Is Nothing Then....

Host Guest_Aylar
A: Are you using Exception exc = Server.GetLastError(); to get the excepetion? Also have you set <customErrors mode="On">? mode="Off"

Host Guest_Aylar
</end>

Host Guest_jperry_ms
For those of you just joining us, we are chatting about ASP.NET Error Handling. Your questions are welcome. Please post them in the Input Room at the bottom of your screen.

Host Guest_jperry_ms
Q: BWaide: I try. But sometime it is difficult because of my last question. Sometimes the docs say nothing about the thrown execption of a framework method!

Host Guest_Doug_Seven_MVP
A: You can have multiple Catch blocks. SO you can try to catch specific exceptions, the at the end catch System.Exception for anything else.

Host Guest_jperry_ms
Q: carl says: I do specific exception when possible, and am trying to implement general in Application_Error but cannot get it to work, even though I'm following textbook examples such as Debugging ASP.net page 60.

Host Guest_Donny_Mack_MVP
A: Are you doing any other exception handling? I believe that the only way it's fired is if you don't handle the exception anywhere else. An "Unhandled Exception"

Host Guest_jperry_ms
Q: kierepka says: Is stringbuilder faster then response.write?

Host Guest_Doug_Seven_MVP
Q: This is more of a performance question, but I'll hit ti here. StringBuilder is more performant than concatenating strings because when you concat, you create multiple string object. With a StringBuilder, you construct the string in one object.

Host Guest_Doug_Seven_MVP
A: Response.Write is a whole different this. You can Response.Write(myString) or Response.Write(myStringBuilder.ToString()) for the same perf.

Host Guest_jperry_ms
Q: LugNut: What if I want to second guess the execption before it occurs...

Host Guest_Doug_Seven_MVP
A: If you are predicting a specific exception, such as a DivideByZeroException you can look it up in the docs. You have to have an idea of what type of exception you are expecting. It is a good practice to always end with Catch ex As System.Exception.

Host Guest_jperry_ms
For those of you just joining us, we are chatting about ASP.NET Error Handling. Your questions are welcome. Please post them in the Input Room at the bottom of your screen.

Host Guest_jperry_ms
Q: barak What bugfixes/ improvements have been made in this area for .NET SDK 1.1?

Host Guest_jperry_ms
A: Not sure which area you're talking about, baraka...1.1 isn't totally nailed down yet, though.

Host Guest_jperry_ms

Q: LugNut: Donny: Just skimmed that article. Can you explain why you would purposefully throw an exception instead of advising the user how to correct the error.

Host Guest_Doug_Seven_MVP
A: In the article it is done for demo purposes. In real life, an example of when you want to throw an exception yourself would be in a transaction method. If you are using AutoCommit, the transaction will automatically abort when there is an exception.

Host Guest_Doug_Seven_MVP
A: Another example would be in an n-tier environment, when you want a middle-tier object to throw an exception for the presentationlayer to handle.

Host Guest_Doug_Seven_MVP
We have about 10 minutes left for more questions.

Host Guest_jperry_ms
Q: barak: sorry the area of error/exception handling

Host Guest_jperry_ms
A: Were there specific areas you think need to be improved? Always looking for suggestions :-)

Host Guest_jperry_ms
whatabohr:

Q: I have heard that there is some performance penalty associates with SEH as opposed to simply returning an error code in what situations should I use each?

Host Guest_Doug_Seven_MVP
A: There is a perf hit when you use a Try/Catch, but depending on the needs of the app, the perf hit may be worth not crashing the app. If you can have a return value and process based on that, then you don't need SEH.

Host Guest_Doug_Seven_MVP
A: SEH is really for blocks of code that could error...i.e. on a database command execution, etc.

Q: alphawizard: I am using MS app block for exception handling in ASP.NET app. It appears that , if I install my custom exceptions in GAC I have to use fully qualified assembly names to reference same in the web.config - any way to use parital name?

Host Guest_Donny_Mack_MVP
Q: can you please explain this a little more: if I install my custom exceptions in GAC I have to use fully qualified assembly names to reference same in the web.config - any way to use

Host Guest_Donny_Mack_MVP
A: ( if I am following you correctly you can just import the namespace into the class)

Host Guest_jperry_ms
Q: whatabohr : so is there any performance hit in entering a try block in the case where an exception is not thrown?

Host Guest_Doug_Seven_MVP
A: I believe there is a small hit, but no more that an If x = True Then...

Host Guest_jperry_ms
Q: whatabohr:I guess what my question is, is whether I can effeciently place a try block within a closed loop, or whether I should force it outside the loop

Host Guest_Doug_Seven_MVP
A: You can place it in a loop liek you are saying. That is fine and acceptable.

Host Guest_Doug_Seven_MVP
A: Do it outside the loop if the If... might trigger an exception.

Host Guest_jperry_ms
Q: whatabohr: but is the overhead of the opening of the try block going to be significant if I execute it in a closed loop the difference here is that in one, I only open the try block once, whereas the other, every time I loop

Host Guest_Doug_Seven_MVP
A: The SEH in the loop is fine. This will keep the loop going even if one item fails. If you want to stop the loop all together, wrap the loop in a Try/Catch

Host Guest_jperry_ms
This has been a GREAT chat. Thank you to everyone. Unfortunately, it is time to go.

Host Guest_Doug_Seven_MVP
Thanks every one...see ya at https://www.dotnetjunkies.com

Host Guest_Donny_Mack_MVP
Goodbye all - thanks for coming. It has been fun! Please email offline if you have any further questions.

Top of PageTop of Page