Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more elegant way to do error handling than spreading try/catch in the entire app code?

I've found myself doing too much error handling with try\catch statements and getting my code ugly with that. You guys have any technique or framework to make this more elegant? (In c# windows forms or asp.net).

like image 751
Diego Correa Avatar asked Nov 18 '10 16:11

Diego Correa


People also ask

How do you handle errors without try catch?

The best alternative I can give you is Elmah. http://code.google.com/p/elmah/ It will handle all your uncaught errors and log them. From there I would suggest fixing said errors or catching the specific errors you expect and not just catch any error that might occur.

Can I use if else instead of try catch?

Try/catch is used when an Exception can be thrown from the code, and you catch it in the catch-clause, which is an object oriented way to handle errors. You can't catch Exceptions with if/else blocks - they share nothing with try/catch.

What are the different types of error handling techniques?

Learn about the four main error handling strategies- try/catch, explicit returns, either, and supervising crashes- and how they work in various languages.


2 Answers

You need to read up on structured exception handling. If you're using as many exception handlers as it sounds then you're doing it wrong.

Exception handling isn't like checking return values. You are supposed to handle some exceptions in limited, key spots in your code not all over the place. Remeber that exceptions "bubble up" the call stack!

Here is a good and well-reviewed CodeProject article on exception best practices.

like image 141
Paul Sasik Avatar answered Oct 04 '22 04:10

Paul Sasik


Java land had pretty the same problem. You just look at method and you can't at a first glance understand what it is doing, because all you see is try/catch blocks. Take a 30-40 line method and throw away all try statements and catch blocks and you might end up with 5-6 lines of pure application logic. This isn't such a big problem with C# as it has unchecked exceptions, but it gets really ugly in Java code. The funny thing is the try/catch blocks were intended to solve the very same problem in the first place. Back then it was caused by errno/errstr madness.

What the Java guys usually do is based on how do you typically handle exception. Most of the time you can't really do anything to correct the problem. You just notify the user that whatever he was trying to do didn't work, put back application in a certain state and maybe log and exception with complete stacktrace to log file.

Since you handle all the exceptions like this, the solution is to have a catch-all exception handler, which sits on top of application stack and catches all exceptions that are thrown and propagated up the stack. With ASP.NET you might use something like this:

http://www.developer.com/net/asp/article.php/961301/Global-Exception-Handling-with-ASPNET.htm

At the same time you are free to override that global handler by placing try/catch block in your code, where you feel something can be done, to correct the problem.

like image 20
Jacek Prucia Avatar answered Oct 04 '22 03:10

Jacek Prucia