is putting all code in try catch blocks runtime error safe?

ethicalhacker

Well-known member
Joined
Apr 22, 2007
Messages
142
Location
Delhi,India
Programming Experience
5-10
If all codes are put into try catch blocks will the program be free from runtime errors crashing the application or hanging the computer?
Users are of many kinds they may enter a character in a number field or many such other things can occur that can give runtime errors or exceptions. The pc may hang or data may be lost or a nasty windows error dialog may be shown. So will putting all code in try catch blocks will do any good?
 
It's a good idea to have a global exception handler, so your app doesn't simply fall over. VB.NET provides this functionality in the UnhandledException event of the application. That allows you to at least notify the user of the fatal error in a friendly fashion and even give them the opportunity to restart the app.

That said, you should only be wrapping Try...Catch blocks around sections of code that can reasonably throw exceptions. By "reasonably" I mean that they try to access something that is beyond your control, like opening a file or connecting to a database.

If the user enters a letter where a number is expected and your app crashes then that's just plain bad programming. Code that might use that value can NOT reasonably throw an exception because you should be using the likes of TryParse methods to test the value first. If it isn't valid input then display a message telling the user so and prompting them to try again.
 
Back
Top