C# Console Error Handling
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might c# console parameter handling have Meta Discuss the workings and policies of this site About
C# Console Error Output
Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads c# error handling in constructor with us Stack 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 c# error handling get line number programmers, just like you, helping each other. Join them; it only takes a minute: Sign up .NET Global exception handler in console application up vote 121 down vote favorite 39 Question: I want to define a global exception handler for unhandled exceptions in my console application. In asp.net, one can define one in global.asax, and in
C# Error Handling Framework
windows applications /services, one can define as below AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyExceptionHandler); But how can I define a global exception handler for a console application ? currentDomain seems not to work (.NET 2.0) ? Edit: Argh, stupid mistake. In VB.NET, one needs to add the "AddHandler" keyword in front of currentDomain, or else one doesn't see the UnhandledException event in IntelliSense... That's because the VB.NET and C# compilers treat event handling differently. c# .net vb.net console console-application share|improve this question edited Jan 30 '14 at 17:09 asked Jun 28 '10 at 14:25 Stefan Steiger 26.2k38185286 add a comment| 4 Answers 4 active oldest votes up vote 174 down vote accepted No, that's the correct way to do it. This worked exactly as it should, something you can work from perhaps: using System; class Program { static void Main(string[] args) { System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper; throw new Exception("Kaboom"); } static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(e.ExceptionObject.ToString()); Console.WriteLine("Press Enter to continue"); Console.ReadLine();
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums https://msdn.microsoft.com/en-us/library/ff953192(v=pandp.50).aspx Blogs Channel 9 Documentation APIs and reference Dev centers Retired content Samples We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. https://bytes.com/topic/c-sharp/answers/233743-catch-all-exceptions-console-based-program Enterprise Library Enterprise Library 5.0 – May 2011 Developer's Guide Developer's Guide Chapter 3 - Error Management Made Exceptionally Easy Chapter 3 - Error Management Made Exceptionally error handling Easy Chapter 3 - Error Management Made Exceptionally Easy Foreword Preface The Team Who Brought You This Guide Copyright and Terms of Use Chapter 1 - Welcome to the Library Chapter 2 - Much ADO about Data Access Chapter 3 - Error Management Made Exceptionally Easy Chapter 4 - As Easy As Falling Off a c# error handling Log Chapter 5 - A Cache Advance for your Applications Chapter 6 - Banishing Validation Complication Chapter 7 - Relieving Cryptography Complexity Chapter 8 - An Authentic Approach to Token Identity Appendix A - Dependency Injection with Unity Appendix B - Dependency Injection in Enterprise Library Appendix C - Policy Injection in Enterprise Library Appendix D - Enterprise Library Configuration Scenarios Appendix E - Encrypting Configuration Files TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Chapter 3 - Error Management Made Exceptionally Easy Introduction Let's face it, exception handling isn't the most exciting part of writing application code. In fact, you could probably say that managing exceptions is one of those necessary tasks that absorb effort without seeming to add anything useful to your exciting new application. So why would you worry about spending time and effort actually designing a strategy for managing
help? Post your question and get tips & solutions from a community of 418,461 IT Pros & Developers. It's quick & easy. Catch All Exceptions for Console Based Program P: n/a Simon Johnson Recently, a thread appeared which asked how to create a "catch all" method for when an exception occurs within a program. The solution given only works for Windows Forms applications, as far as i can tell, is it possible to do the same with a console application? Simon. Nov 15 '05 #1 Post Reply Share this Question 5 Replies P: n/a Morten Wennevik put all code in Main() inside a try/catch(/finally) block. Main is where your program starts and any exception not caught elsewhere in your code will end up in main before your program crashes. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ Nov 15 '05 #2 P: n/a Nicholas Paldino [.NET/C# MVP] Morten, That's not completely true. You can have multiple threads in a console application, and if an exception is thrown on one of those threads, then you will not catch it by wrapping the code in a try/catch block. Take for example the following code: static void Main(string[] args) { try { // Create a thread. Thread pobjThread = new Thread(new ThreadStart(DoSomething)); // Start the thread. pobjThread.Start(); // Wait five seconds. Thread.Sleep(5000); } catch (Exception e) { Console.WriteLine("Exception on Main thread:") Console.WriteLine(e); } // Get out. return; } private static void DoSomething() { // Throw an exception. throw new Exception(); } This will crash and not be caught by the catch block. Rather, you need to attach to the UnhandledException event on the current AppDomain, like this: static void Main(string[] args) { // Wire up an event handler for unhandled exceptions. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException) ; try { // Create a thread. Thread pobjThread = new Thread(new ThreadStart(DoSomething)); // Start the thread. pobjThread.Start(); // Wait five seconds. Thread.Sleep(5000); } catch (Exception e) { Console.WriteLine(e); } // Get out. return; } private static void UnhandledException(object sender, UnhandledExceptionEventArgs e) { // Write the exception. Console.WriteLine("AppDomain Unhandled Exception Event Handler:"); Console.WriteLine(e.ExceptionObject); } private static void DoSomething() {