Error Handling Strategy C#
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site c# error handling in constructor About Us Learn more about Stack Overflow the company Business Learn more about
C# Error Handling Get Line Number
hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
C# Error Handling Framework
the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What should be the
C# Error Handling Techniques
best Exception Handling Strategy up vote 10 down vote favorite 2 I am working on application where user invokes a method from UI , on this I am calling a method from business class which calls another methods UI--> Method1 -->Method2 --> Method3 I want to display the error message to user if any exception occurs in any of the method. Shall I go with throwing c# error handling class the exception directly to the caller method and at the UI layer I will catch exception and display the message. Apart from Throwing exceptions and catching it at caller is there any better way to handle it? I do not want to use the C++ convention where integer is returned as the result. c# design design-patterns share|improve this question asked Jul 7 '10 at 10:09 Ram 4,31283773 add a comment| 7 Answers 7 active oldest votes up vote 3 down vote accepted Given your example of UI--> Method1 -->Method2 --> Method3, I would make sure that the UI has a try/catch, but then none of the other methods should catch exceptions unless they can handle them without re-throwing. And even if they handle the exception, you should question whether that exception should happen in the first place. If you handle an exception and go about your merry way, then that exception is part of your normal process flow which is a serious code smell. Here are my recommendations: 1) Put your exception handling code in all your UI events, and then have the actual action farmed off to some other method. Don't scatter exceptio
commonplace to earlier releases. Of course, handling errors is part of the job, but new implementations in error handling in c# best practices C# 6 can diminish the extra work. Bill Wagner, author of Effective error handling c# mvc C#: 50 Specific Ways to Improve Your C#, Second Edition, details the changes in the new release. From error handling in asp.net c# the author of From the author of Effective C# (Covers C# 4.0): 50 Specific Ways to Improve Your C#, 2nd Edition Learn More Buy From the author of From the http://stackoverflow.com/questions/3193772/what-should-be-the-best-exception-handling-strategy author of Effective C# (Covers C# 4.0): 50 Specific Ways to Improve Your C#, 2nd Edition Learn More Buy Exception filters have been added to C# 6 as part of the "co-evolution" strategy for C# and VB.NET. Exception filters have been present in VB.NET for several releases, and have also been available in F#. They're new for C# in version 6. http://www.informit.com/articles/article.aspx?p=2424332 New Behaviors for Exception Filters Exception filters now provide more general control over when an exception should be handled. In previous releases, the only way you could indicate to handle an exception was based on the type of exception. You caught all exceptions of a given type, including all exception classes derived from that given type. That syntax might not always cause limitations, but in several scenarios it resulted in more cumbersome error-handling code. Those scenarios are addressed by the exception filters in C# 6. Using the new syntax for exception handlers is easy: Just add a when clause to a catch handler. If the expression following the when keyword evaluates to true, the catch block runs. If the expression following the when keyword evaluates to false, the exception continues to propagate: try { DoSomeWork(); } catch (SomeException e) when (passesFilter(e)) { // Handle errors.... } The syntax for exception filters was revised as this release was being developed. The original syntax used if rather than when: // Original syntax used 'if' instead of 'when' try We would catch it and write it to a log. More sophisticated versions http://mikehadlow.blogspot.com/2009/08/first-rule-of-exception-handling-do-not.html even featured a stack trace. Yes, really, we were that advanced :) http://www.infoworld.com/article/2896294/c-sharp/best-practices-in-handling-exceptions-in-c.html The reason we did that was simple, VB didn't have structured exception handling and if your application threw an unhandled error it simply crashed. There was no default way of knowing where the exception had taken place. .NET has structured exception handling, but the VB mindset error handling of wrapping every piece of code in a try-catch block, where the catch catches System.Exception, is still common, I see it again and again in enterprise development teams. Usually it includes some logging framework and looks something like this: try { // do something } catch (Exception exception) { Logger.LogException("Something bad just happened", exception); } Catching System.Exception is c# error handling the worst possible exception handling strategy. What happens when the application continues executing? It's probably now in an inconsistent state that will cause extremely hard to debug problems in some unrelated place, or even worse, inconsistent corrupt data that will live on in the database for years to come. If you re-throw from the catch block the exception will get caught again in the calling method and you get a ton of log messages that don't really help you at all. It is much better to simply allow any exceptions to bubble up to the top of the stack and leave a clear message and stack trace in a log and, if possible, some indication that there's been a problem on a UI. In fact there are only three places where you should handle exceptions; at the process boundary, when you are sure you can improve the experience of the person debugging your application, and when your software can recover gracefully from an expected exception. You should always catch exceptions at the proc takes a cloud service to manage cloud services Security for your collaborative software Review: TensorFlow shines a light on deep learning More Insider Sign Out Search for Suggestions for you Insider email Analytics All Analytics Big Data Business Intelligence Application Development All Application Development Java JavaScript Node.js Careers Cloud Computing All Cloud Computing Cloud Storage IaaS PaaS SaaS Collaboration Databases All Databases Hadoop NoSQL Datacenters All Datacenters Devops Disaster Recovery Systems Management Hardware Internet of Things Mobile All Mobile Android BYOD Mobile Apps Mobile Development Mobile Management iOS Networking All Networking Internet SDN Wi-Fi Open Source Operating Systems All Operating Systems Linux MacOS Microsoft Windows Security Software All Software Browsers Desktop Software Office Software Storage Virtualization See All Technologies News Blogs Reviews Tech Watch Insider articles Newsletters Deep Dives Slideshows Video Resources/White Papers × Close Home Application Development IDG Contributor Network Want to Join? Microsoft Coder By Joydip Kanjilal star Advisor Follow Best practices in handling exceptions in C# More like this Exception handling in WCF Implementing a Custom Exception class in C# New features in C# 6 on IDG Answers How to handle tough feedback at work? Best practices in exception handling Take advantage exception handling and use it judiciously to handle runtime errors in your application Email a friend To Use commas to separate multiple email addresses From Privacy Policy Thank you Your message has been sent. Sorry There was an error emailing this page. Comments InfoWorld | Mar 12, 2015 Like this article? thumbsup 0 thumbsdown RELATED TOPICS Application Development C# Comments Exception handling is the technique of handling runtime errors in your application code. Basically, you have two categories of exceptions: Exceptions that are generated by the application and those that are generated by the runtime. Exceptions should be handled with care -- you should have a good idea of how exceptions should be handled and when they are needed to be handled in your co