If we have a class main and the constructor of main constructs our setupclass and we put this class in a try catch in main.
Theoratically Will all errors go into catch? If they will how will it effect the performance and mantainability? Will the application be crash free?
8
Your program can still crash or otherwise fail to perform it’s tasks:
-
Your exception handling code could have a bug in it; by definition your handler is outside of the code ‘protected’ by your
try
/catch
, so an error there will still result in a program crash. -
Just because you handled the exception, doesn’t mean you your program state is still sane. Your program could easily end up in a state that is effectively useless, because it’s internal state is left in an inconsistent state. You program will then continually throw exceptions, and the end user will be left with no other option but to terminate your program forcibly.
Moreover, because you catch all exceptions, you’ll have a much harder time debugging what actually goes wrong in your program, and you can never be sure if the exceptions you see are the results of broken internal state after a previous exception was handled, or a genuine bug.
Better to let your program crash. At least then the end user understands the program broke, and you as a developer will have a much better sense of what broke the program.
1
Yes and no. I work on embedded systems. When an unhandled exception occurs we log it and reboot the system, very similar to what you’re suggesting. Maybe 9 times out of 10 the reboot clears up the problem, at least good enough while the exception report is being analyzed for a fix. The other 1 time in 10 we get a continuous reboot cycle, which is enough of a problem unto its own that we keep track of how many reboots have happened and go into a “recovery” mode after a few tries.
At any rate, it’s no panacea. It’s a small bandage you apply to give you time to fix it for real, and it’s only helpful if you get those exception reports and have solid initialization code that truly resets the state of your system.
Depends on languaga but probably not.
In c# then other threads generating exceptions will kill the main process off the top of my head.
It’s irrelevant though, except for very simple programs, you’re likely to end up in an irrecoverable state resulting in bad or lost data which for most users is much worse than the application crashing.
3
The appropriate goal is not to make your application “crash free” but to make your application reliable and stable.
It may be possible to reduce or even virtually eliminate crashes with exception handling. However, all other things being equal, this will simply replace crashes with cases in which the program enters an unusable state.
This isn’t going to look any better to your users. In fact, it will very likely be worse. At least a crash is a clear, clean failure. A program in a non-functioning state is ambiguous at best. The user may waste a long time to see if the application is frozen or just “thinking”. At worst it may actually do damage, continuing to perform actions, but in a corrupt manner.
There is no shortcut to writing good, reliable code.