Adding further exception handling to an application with a single exception handler?

I think you're attempting to shut the barn door after the proverbial horse has bolted Exception handling isn't something you can easily bolt-on as an afterthought Consideration should be given to exception handling as you build your application. For every functional unit of code that you write you should carefully consider whether exceptions could be generated and if so identify the appropriate place to handle them. Certainly there's no magic library you can just plug in Now I required to add exception handling mechanism, currently my application have no try catch anywhere except in Main().

Where I put try catch, and handled all types exception in Global exception handler(ApplicationThreadException). But this is not proper way to handle exception In catching all exceptions in main you've (so far) made the decision that your app doesn't need to deal with them at any other level.Is that actually the case? Ask yourself, whether your application currently functions well and does what it's supposed to do without constantly erroring out Do you really need to add all of that exception-handling code?

If your app functions well now and you don't see any problems, then the answer is probably no: you've already got all of the exception handling that your currently need However, if your app is buggy and regularly fails with some exception or other being caught in main() and you're thinking to yourself that the whole app shouldn't fail just because of recoverable error, then you've identified a case for exception handling elsewhere, and you should go back and identify the individual failure cases and address them at the appropriate level in your app.

I think you're attempting to shut the barn door after the proverbial horse has bolted. Exception handling isn't something you can easily bolt-on as an afterthought. Consideration should be given to exception handling as you build your application.

For every functional unit of code that you write you should carefully consider whether exceptions could be generated and if so identify the appropriate place to handle them. Certainly there's no magic library you can just plug in. Now I required to add exception handling mechanism, currently my application have no try catch anywhere except in Main().

Where I put try catch, and handled all types exception in Global exception handler(ApplicationThreadException). But this is not proper way to handle exception.In catching all exceptions in main, you've (so far) made the decision that your app doesn't need to deal with them at any other level. Is that actually the case?

Ask yourself, whether your application currently functions well and does what it's supposed to do without constantly erroring out.Do you really need to add all of that exception-handling code? If your app functions well now and you don't see any problems, then the answer is probably no: you've already got all of the exception handling that your currently need. However, if your app is buggy and regularly fails with some exception or other being caught in main() and you're thinking to yourself that the whole app shouldn't fail just because of recoverable error, then you've identified a case for exception handling elsewhere, and you should go back and identify the individual failure cases and address them at the appropriate level in your app.

You are right , Hats off. Actually due to some time constraints I couldn't find out my way to implement proper exception handling while developing application. I want to share one more point, in future this application will have feature like sending error report via email with screen shot,so want some reference for designing it, want to see how exception are handled in enterprise application .

So from reference I can find out my way, Thanks again. – Miraj Baldha Sep 15 at 17:27 1 It should be part and parcel of every functional unit of code that you write. Yikes.

Couldn't agree less. I find very few units of code warrant any exception handling. I place exception handling code at a few strategic points.90% or more of the methods I write simply let their exceptions bubble up to one of those points.

Your statement would only be valid for me if I wrote huge methods which massively violated SRP. – Charles Sep 15 at 20:02 @Charles: +1 Thanks. I've edited my copy to reflect that sentiment, because that's really what I meant.

I didn't mean to suggest that every little chunk of code has an exception handler, but rather that consideration needs to be given to exception handling and implementing it in the appropriate places as the code is written, rather than being bolted on at the end. – razlebe Sep 15 at 20:05 If your app functions well now and you don't see any problems, then the answer is probably no: you've already got all of the exception handling that your currently need. I more or less agree with this, but it contradicts what you said previously.

I also diagree that the horse has bolted. Exception handling is one aspect of developement that I find isn't especially difficult to add after the fact. However that does require having written good SOLID code to start with.

– Charles Sep 15 at 20:09 Okay, the edit you did while I was writing that last comment makes the first half of that comment obsolete (you are no longer contradicting yourself). Good! :) (+1) – Charles Sep 15 at 20:11.

Only the logic of operation of your application tells you where to use try/catch. If you can do something in case of exception - then you put the statement at that point. If in an entire app you didn't feel like you needed exception handling, because there were simply nothing you could do with them - outermost catcher is a good place for your exception handling, but would only serve logging purposes.

Wire up event handlers to handle unhandled exceptions anywhere in your application and then don't use a try/catch in main. Try this in your Main(): //Wire up handling of all unhandled exceptions AppDomain.CurrentDomain. UnhandledException += CurrentDomain_UnhandledException; Application.

ThreadException += new System.Threading. ThreadExceptionEventHandler(Application_ThreadException); Application. SetUnhandledExceptionMode(UnhandledExceptionMode.

CatchException); //Handle the events static void Application_ThreadException(object sender, System.Threading. ThreadExceptionEventArgs e) { HandleException(e. Exception); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { HandleException((Exception)e.

ExceptionObject); } public static void HandleException(Exception e) { //Do something with the exception stored in e. //Email an admin, show the user a pretty message, show the user a detailed stack trace only if they click Show Details, etc. }.

For more details on this see the source post (also from this site): stackoverflow. Com/questions/1762622/… – Matt Cofer Sep 15 at 17:02 Here code seems interesting let me see how can I play with it – Miraj Baldha Sep 15 at 17:30.

I always use this code : public static void HandleException(Exception e) { string s="Message= "+e. Message +"\n"; s+="Source= "+e. Source +"\n"; s+="Stack Trace= "+e.

StackTrace+"\n"; Exception inner=e. InnerException; MessageBox. Show(s); while(inner!

=null) { string ss="Message= "+inner. Message +"\n"; ss+="Source= "+inner. Source +"\n"; ss+="Stack Trace= "+inner.

StackTrace+"\n"; MessageBox. Show(ss); inner=inner. InnerException; } }.

Your way is surely not proper because you catch the exception far away from the point where it was thrown. And your global catch can eventually log but is unable to react. Imagine situation where you try to create a file in a certain folder before manipulating it, it could be that you decide to catch the exception if the folder does not exist and you first create the folder then create the file in it.In the Main method is too late because you can't do anything, you can log yes but you can't create the folder, even if you do, the program will not continue from the proper point anyway.

This is veeeery vague introduction to why to do proper exception handling, just an example out of very many... just put your try catch everywhere needed and either react to exception (handle them) or throw them (sometimes from inside a catch. Sometimes omitting the catch clause at all)... you should read some articles on this I think.

Thanx for sharing info. Actually I am trying to figure out my way, but can't find, I seen lots of articles. But I am not satisfied, means things are just gone out my way, can you give me urls for reference?

I stuck at this. – Miraj Baldha Sep 15 at 17:24 -1: please clarify that exceptions should only be caught when there is a way to actually handle the exception. I'm concerned that someone reading this will assume you meant them to catch exceptions at every point in the program, which is surely not what you intended to say.

– John Saunders Sep 15 at 17:27 Please read again, I said in some cases you omit the catch meaning exactly what you said. You catch if it makes sense or you do nothing or you do other things... sounds clear to me – Davide Piras Sep 15 at 17:32.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions