I've got a basic idea about exceptions recently. There is one thing which I can't get and didn't see in examples is what is a good practice for keeping exception classes, I'm about developer-made exceptions. Is it a good way to write an exception class in the file of the class whose method(s) will invoke this particular exception OR it is better to create a separate file with a list of all exceptions your code can possibly invoke? What is your guys approach on this?
There are always some benefit to use a separate file for declaring exception.
This can be understood when we have a large codebase and user defined exception. It will not matter so much when you are checking with a small file.
But suppose we create a exception(such as a exception when we insert some invalid values in DB, specific user defined exception we be better than SQL exception to debug). This will be generic for all classes. We will like to have the code to log this exception in a specific file so that we cant rewrite the code again and again.
I disagree Campbell. I think Exceptions should be coupled with the package where they make most sense. Like IOExceptions are in the java.io packages.
It's a bit like making an interface for just constant values. I think it's bad practice.
Campbell idea and Stephan one are the best. For all global exception which you will use all over the application, you can build exception package. for specific exceptions which you may use with special classes you declare it in the same package.