Web Development Web Design Partners Software Development

Try Catch vs. On Error Resume Next Error Handling

Unlike C#, VB has a feature that lets your code execute the next line after an error on the previous. This statement in VB is On Error Resume Next. It seems to be a hotly debated argument with supporters on both sides of the fence. Usually, the argument is that they rather return a code error on the page to the visitor so that the visitor is aware of the error. However, this is not always the best idea and it looks unprofessional. In my opinion, this On Error.. statement is actually very useful, if properly implemented, as it has a finer grain of error handling. Let's see what it looks like:


The On Error Resume Next statement looks something like this:

On Error Resume Next
Statement #1
Statement #2
Statement #3


Essentially, lets say that an error occurs on the Statement #1 line. Because On Error Resume Next was called in the beginning, the next line Statement #2 is executed and so on.


Now lets take a look at the Try Catch statement

try
{
Statement #1
Statement #2
Statement #3
}catch(Exception ex)
{
//do something with error here
}


In a Try-Catch statement, where an error occurs on Statement #1, statements #2 and #3 are skipped and the Catch section is executed. The only way to have statements #2 and #3 to execute would be to put every statement in its own Try-Catch block. Of course this would be very difficult to read and very tedious to do. A model class can easily be 1000 lines long and to put in a Try-Catch block every 5 lines would still be 200 blocks. Of course in reality practically nobody would actually do this. This means that with any error that occurs, it's guaranteed that some lines of code are going to be skipped which could cause a larger problem than the error itself.


Let me give you a real world example that I experienced. I've been developing e-commerce websites for a while now. However, when I started back around 2001 I was using ASP and the On Error Resume Next statement. It would log every error to the database and send an error email to me for review. Not too long after publishing a new ecommerce website, a customer put in an order and an unexpected error occured in processing the credit card with a HTTPS connection to the merchant gateway. This was not just a status error with the http client but rather an actual VBScript error. If the On Error Resume Next statement was not used, the customer would have seen an error message on a standard error page and the order would have failed. In that particular case, the next line of code executed and the order was saved into the database. Because the card was not charged, the order details page stated as such for the customer service representative. The customer service representative then retried the card with the gateway, and the card was approved. The customer never saw any error.


Now imagine that the On Error Resume Next statement was not in use. In that case, the customer would be stuck with either a generic error page or a detailed code error the customer doesn't understand. Not only that, the standard error message page doesn't look anything like the ecommerce website. It would have looked unprofessional and the customer would have to call to check and see if the order went through. The problem is that customers would probably reconsider whether to do business with your company if they see any error like that during the ordering process. Can they trust their credit card details with you if your application is generating errors? On top of that, it could be afterhours and the customer would have to call back the next day. At minimum, it would either be a loss of a customer to the business, or an inconvenience to the customer that the order couldn't be placed online.'


Now lets say that this ecommerce website was witten in C#. With a Try-catch block it could work if done properly but it would end badly if you didn't expect something odd to occur. If you use one Try-Catch block for your code and the connection to the gateway is in the same block as the 'save order' code line, then the error would have skipped the call to save the order. Since you got an error message, you could call the customer and see if he/she still wants to put it through but this doesn't look as good. It's also an inconvenience and could delay delivery. The right way to implement this section of the code would be to put the gateway connection in its own Try-Catch block. That way, the rest of your code is not skipped.


Unfortunately, sometimes a completely unexpected error is generated where you did not isolate a line with a Try-catch. In those cases, you will have to make your corrections to the code afterwards. It is in those scenarios that the On Error Resume Next advantage becomes apparent and sometimes these errors do occur. Visitors and website crawlers/hackers post data to your site that is not expected. In most cases, the error is small and doesn't warrant a complete failure. Take for instance if a NullReference exception occurs because a field on the order was missing or an error because of an illegal character in a field. Would it be better to let the site result in an order failure? Or, would it be better to have the customer representative contact the customer for the missing field (presuming it's necessary which might not be the case)? From a business perspective, the latter scenario is better.


It's like driving your car on the highway and pulling over to the side because there's a nail in your tire resulting in an air leak. You are better off driving to the auto shop and not letting a small inconvenience cause a larger problem. C# has its advantages, but the lack of a feature like VB's On Error Resume Next is not one of them. The Try-catch statement tends to clutter the rest of your code and the extra time it takes is money.


This article was written by Roger DeSousa and published here on 3/25/2019.

Website Owners

Would you like free advice?
Website construction doesn't have to cost you a lot
Custom development vs. Boxed software
Custom design vs. Premade templates
Why it's harder today to rank high on search engines
For small budgets, have your developer integrate open source software
Integrate a point of sale (POS) and your website without breaking your bank

Services for Web Designers & Agencies

Add web development to your services
A developer with SEO experience could be good for your design

Technical Talk

Try Catch vs On Error Resume Next
Microsoft technology vs. PHP
Do-it-yourself responsive design vs. Boostrap
Will Microsoft lose all the new web developers?

Are you a web designer or ad agency? We'll develop for you

We like working with other design professionals. Both design and development are skills that take time to master and it's rare that one person is very good at both. Most professionals simply choose to invest their time in one over the other due to a personal or business preference. Although we have done both at Etchedweb in the past, we lean more on the development side of the fence and would like to focus on just that with our professional partners.

Likewise, we realize there's many web design companies, ad agencies, and other professional companies out there that currently do not do any development work but might seek out those projects if they have someone to work with. A partnership of this nature would enable you to concentrate on your forte but expand your services and allow you to design for projects that require both. We will offer you this service at rates you can reasonably mark up and profit from. Best of all, you don't need to have any technical skills to work with us. You get to keep your client and work directly with him/her. We'll help you along the way to successfully complete the job.

If you are interested in exploring a partnership of this nature and would like to find out more, please contact Roger at roger@etchedweb.com. In your email, please tell us briefly your name, your company's name, and the type of work you do.

Website Owners

Would you like free advice?
Website work doesn't have to cost you a lot
Custom development vs. Boxed software
Custom design vs. Premade templates
Why today it's harder to rank high on search engines
For small budgets, have your developer integrate open source software
Integrate a point of sale (POS) and your website without breaking your bank

Services for Web Designers & Agencies

Add web development to your services
A developer with SEO experience could be good for your design

Technical Talk

Try Catch vs On Error Resume Next
Microsoft technology vs. PHP
Do-it-yourself responsive design vs. Boostrap
Will Microsoft lose all the new web developers?

You Do The Web Design And Ad Agency, We'll Take Care Of The Development?

Etchedweb, LLC was originally formed in Boulder, CO in June of 1999. Since 1999, we have provided the highest available service to many small businesses at reasonable web development rates. Many of our long time customers enjoyed top search engine rankings for over a decade with great returns on their investment.

We are primarily a Microsoft development shop developing on the ASP.NET and ASP platforms with SQL Server databases. We have a long track record of working in the e-commerce field, and we have a lot of good advice for companies in that field. E-commerce is a 24 hour/ 7 day a week business requiring reliable software and hardware to maintain availability for your customers, search engines, and your business partners. We do work in other fields other than e-commerce too and can bring the lessons we've learned in e-commerce to benefit your company.

Feel free to contact us to discuss your business's internet development requirements. We'll be happy to discuss your project with you and provide rates and quotes to complete your project. Your Satisfaction Is Guaranteed

If for some reason you are not satisfied with our work, please let us know and we'll make the corrections or give you your money back FONT anonymous pro

Skills and tools we work with C#, ASP.NET MVC 5, Core 2.2, Web Forms, Web API, ASP, Javascript, Visual Basic ADO.NET, MS SQL Server & SSMS, SQL, TSQL, Stored Procedures, Entity Framework HTML5 & CSS, Responsive Web Design jQuery, Ajax, AngularJS, Windows Script Host JSON, XML IIS, FTP Server, DNS Server, Windows Firewall Visual Studio, Dreamweaver Fireworks, Photoshop, Flash hMailServer, Imail Credit Card Processing, PCI-DSS Azure, Google Cloud
Contact Us About Us