Currently I’m catching only generic exceptions, but i want change this to catch the specific exceptions, but what is the advantage of this?
Currently I’m catching only generic exceptions, but i want change this to catch the
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The difference between performing a general try/catch statement and catching a specific exception (e.g. a FileNotFoundException) typically depend on what errors you need to handle and what errors you don’t need to worry about. For instance:
The code above will catch EVERY exception that is thrown inside of the try statement. But maybe you don’t want to handle every error. What can you do with an “OutOfMemory” exception?
A better method of error handling would be to perform some default action if the error is unknown or something you can’t do anything about, and perform another action if you discover that you can do “Plan B” if you catch.
For example, assume you are trying to open a file, but the file doesn’t exist. You can catch the FileNotFoundException and create a new blank file as below:
This has been the most effective and user-friendly method of error checking that I’ve used in the past.