Error Handling Patterns 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 About Us Learn more about Stack Overflow the company Business
C# Error Management
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation exception handling patterns c# Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
C# Error Handling In Constructor
you, helping each other. Join them; it only takes a minute: Sign up Design Pattern for Error Handling up vote 1 down vote favorite I've ran into this problem a few times on various projects, and I've c# error handling get line number wondered if there's a better solution than the one I normally end up using. Say we have a series of methods that need to execute, and we want to know if something goes wrong within one of the methods and break out gracefully (potentially undo-ing any previous changes...), I typically do the following (pseudo C# because it's what I'm most familiar with): private bool SomeMethod() { bool success = true; string errorMessage = null; success = c# error handling framework TestPartA(ref errorMessage); if (success) { success = TestPartB(ref errorMessage); } if (success) { success = TestPartC(ref errorMessage); } if (success) { success = TestPartD(ref errorMessage); } //... some further tests: display the error message somehow, then: return success; } private bool TestPartA(ref string errorMessage) { // Do some testing... if (somethingBadHappens) { errorMessage = "The error that happens"; return false; } return true; } I just wondered (and this is my question) if there's a better methodology for coping with this kind of thing. I seem to end up writing a lot of if statements for something that seems like it should be slicker. I've been suggested having a loop over a set of delegate functions, but I'd be worried that would be over-engineering the solution, unless there's a clean way to do it. c# asp.net design-patterns error-handling custom-error-handling share|improve this question edited Jan 31 '14 at 9:52 Suhaib Janjua 1,94452448 asked Jan 31 '14 at 9:40 loxdog 5661724 1 If these are truly errors, then you should probably be throwing exceptions. –Polyfun Jan 31 '14 at 9:43 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted I think you should probably be using exceptions. Note you should generally only be catching exceptions at the "top level" in your application. private void TopLevelMethod() { try { SomeMet
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss
C# Error Handling Techniques
the workings and policies of this site About Us Learn more c# error handling class about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack
Error Handling In C# Best Practices
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping http://stackoverflow.com/questions/21476716/design-pattern-for-error-handling each other. Join them; it only takes a minute: Sign up Good practice design pattern for Exception handling up vote 2 down vote favorite I have exception handling code in every method for the below code for the bottom level methods throw new Exception("The error that happens"); Is there any way I can avoid writing this code again http://stackoverflow.com/questions/31060246/good-practice-design-pattern-for-exception-handling and again in each method? I am trying to write my own code and not using any log frameworks private void TopLevelMethod() { try { SomeMethod(); } catch (Exception ex) { // Log/report exception/display to user etc. } } private void SomeMethod() { TestPartA(); TestPartB(); TestPartC(); TestPartD(); } private void TestPartA() { // Do some testing... try { if (somethingBadHappens) { throw new Exception("The error that happens"); } } catch (Exception) { // Cleanup here. If no cleanup is possible, // do not catch the exception here, i.e., // try...catch would not be necessary in this method. // Re-throw the original exception. throw; } } private void TestPartB() { // No need for try...catch because we can't do any cleanup for this method. if (somethingshappens) { throw new Exception("The error that happens"); } } c# .net design-patterns share|improve this question edited Jun 25 '15 at 21:53 farid bekran 1,003622 asked Jun 25 '15 at 20:32 priya 132 do you know the difference between throw & throw new take a
Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question about this article Ask a Question View Unanswered Questions View All http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET Questions... C# questions Linux questions ASP.NET questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS error handling Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help c# error handling What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Articles » Development Lifecycle » Best Practices » General Coding ArticleBrowse CodeStatsRevisions (2)Alternatives Comments (199) Add your ownalternative version Tagged as VBC#Windows.NETDotGNUVisual-StudioVS.NET2003DevQAArchitect Stats 1.4M views1.1K bookmarked Posted 9 Feb 2005 Exception Handling Best Practices in .NET Daniel Turini, 21 Feb 2005 CPOL 4.90 (412 votes) 1 2 3 4 5 4.90/5 - 412 votes19 removedμ 4.77, σa 1.22 [?] Rate this: Please Sign up or sign in to vote. Design guidelines for exception handling in .NET which will help you to create more robust software Contents Introduction Plan for the worst Check it early Don't trust external data The only reliable devices are: the video, the mouse and keyboard. Writes can fail, too Code Safely Don't throw new Exception() Don't put important exception information on the Message field Put a single catch (Exception ex) per thread Generic Exceptions caught should be published Log Exception.ToString(); never log only Exception.Message! Don't catch (Exception) more than once per thread Don't ever swallow exceptions Cleanup code should be put in finally blocks Use "usi