This question already has an answer here:
Let's say I have a few methods that access the File System, and I want them to be a bit robust, I want to throw errors so the user can react:
- If the file doesn't have read / write rights, I want to inform him/her.
- If the file does not exist, I want to prompt him/her to chose another one.
And so forth.
Thing is, the exceptions are cluttering the code a bit:
FileNotFoundException: quite necessary, the file was not found, try again or give up.
IOException: This is usually when the file can't be read for whatever reason.
It was there, but now it's not, you don't have access rights any more, something went horribly wrong. Generally this is a stopping point, but still the user could choose another file or something, I'd rather not crash the app.
InvalidPathException: Either the developer, or the user tried to give a bad path (like including an illegal character in the file name, again, the app should not crash because of this.
In my case I'd say all these exceptions are important because IOE captures FNFE, but they get resolved differently, and while FNFE and Invalid Path are similar, I still have to throw them separately, but at least catch them together.
My question is
When exceptions start piling up, the code starts to get ugly, I'm guessing there's no work around for this?