问题
I was wondering if there is a right place to handle exceptions. Should I handle it inside my method or should I handle it at the method call? Or does it matter at all?
I'm sorry, but I couldn't find anything about this (googling "exception handling scope" didn't returned what I was looking for).
Example:
// this way...
void readFile(string file)
{
try
{
/* do my stuff */
}
catch(Exception exception)
{
/* handle exception */
}
}
int main()
{
readFile(file);
}
// or this way?
void readFile(string file)
{
/* do my stuff */
}
int main()
{
try
{
readFile(file);
}
catch(Exception exception)
{
/* handle exception */
}
}
Thanks in advance.
回答1:
In general you want to handle the error where it makes sense to do so.
If in your example above you want to try to read a file and if that fails then read a default file the you can handle it as in the first example.
If the readFile operation failing is vital to the rest of main() then you need to have the exception passed up to that so it can deal with whatever fallout is for readFile() failing and this would be as in your second example.
Of course you can always handle the error (or some possible exceptions) inside the method and rethrow or let some pass through or whatever.
Really though its your program flow that determines where your exception handling goes. Handle the exception where it makes sense to do so.
回答2:
The first approach is usually better, since all the file stuff is handled by readFile and code calling readFile does not have to worry about file handling issues. You could however return a boolean from readFile telling the caller if the operation succeded:
bool readFile(string file)
{
try {
/* do my stuff */
return true;
} catch(Exception exception) {
/* handle exception */
return false;
}
}
回答3:
If you can handle the exception, recover from it and continue, then you should do that immediately.
On the other hannd, if there is nothing sensible you can do to handle the exception then the best thing is to let the exception propagate up the call stack. Eventually code at the top-level will be forced to catch the exception and log it / show it to the user, or else the application will crash.
回答4:
Only handle the exception if you actually plan on doing something unique in that particular circumstance (e.g. talking to a DB and a deadlock exception occurs you may wish to retry the DB operation). If you just want to do some generic exception handling action (e.g. log every exception) do it in the highest level possible (usually the UI) and don't clutter your code up with exception handlers everywhere - just let the exceptions bubble up.
回答5:
The best practice is to let the upper levels handle the exception. Imagine that you are packing your low level file reader in a library and handling the exception there. You are not giving the users of your code the ability to handle the exception in the way that they desire.
来源:https://stackoverflow.com/questions/8156530/exception-handling-in-method-definition-or-call