Wednesday, April 4

On Error ... resume next?

I am an old style of C programmer, and I am satisfied with the try...catch way in error handling:
try{
An operation that might bring error
}catch{
When there is error, how to deal with it.
}
If an error is threw outside of try...catch block, it is a run-time error, and the program will stop execute.

The Java's error handling is too aggressive. Every error must be handled. If an operation has the probability to bring error, it will be defined to throw an error, then in the program you MUST either use try...catch to handle the error, or throw the same error to other programs. I have to admit that this is a good practice. The rigid rule push you to think of error when programming. In this way you won't feel lost, when the error actually happen.


In Visual Basic/VB.NET/ASP/ASP.NET, it is a different story. "On Error Goto 0" means that if error is threw, stop the program immediately. This is the default setting. Another option is "On Error Resume Next", which means to ignore the error, keep going to execute next line. The program will be:
On Error Resume Next
operations that might throw error
if err.number <> 0 then
display err.description
end if
Why can this bring trouble? Because there is no closing line in this model. When you start using "On Error Resume Next", it will be in this status until the program is finished, or reach "On Error Goto 0" command, which is not common. So from the "On Error Resume Next" to the end of this program, any error will be ignore. If you forgot to handle that in the "if err.number..." block, then no one realizes the existence of this error, and it is not handled at all.

The worst thing is, in VB/ASP development, it is recommended to put "On Error Resume Next" on top of the program, because you should always show beautiful interface/webpage to users. All errors will be covered from users, and programmers too.

Today I read an asp code about calling CDO.Appointment to create meeting in public calendar of exchange server. The program is something like:
On Error Resume Next
.....(some operations)
Conn.Open iMbx.BaseFolder
iAppt.DataSource.SaveToContainer iMbx.Calendar, Conn
if err.number<>0 then
show error message
end if
.....(more operations)
The program worked pretty good in the last 3 years. But last month, when the new Daylight Saving Time applied, we catch error 0x8004011B(Unknow Error). So we believe it has something to do with the DST and the patches we installed during this period. So we searched a lot of articles about DST and Exchange server and CDO control. None of the methods (installing hotfix, editing registry...) works.

Finally I found one obvious mistake in one sentence between "On Error" and "Conn.Open". When I looked up, I found more mistakes. It is a mystery how this code survive for 3 years. I deleted the "On Error Resume Next" and got rid of the errors one by one. Then, the program works happy ever after.


I still like the C language way. If an error is vital, there is no way to cover it, it is OK to let the program die. We only catch the error that we can cover elegantly. We don't cover EVERYTHING, as the VB programmers do. Java pushes us to think too much about dying. I am an optimist, and I don't like that feeling.

Labels: ,

3 Comments:

At May 23, 2007 10:54 AM, Blogger  said...

Excelent article. You really helped me with the error handling schema in VB.

 
At May 24, 2007 8:45 PM, Blogger Ben said...

I'm so glad it helps.

 
At February 20, 2012 7:42 AM, Anonymous Anonymous said...

That would be a great season to apply for jobs. Hope it would be as good for this summer too. For some resume services Calgary, contact Ken Docherty, he personally works with your resume.

 

Links to this post:

Create a Link

<< Home