Search code examples
exceptioncustom-exceptions

Why is it a "bad idea" to throw your own exceptions?


Why is it a "bad idea" to throw your own exceptions?

found here


Solution

  • In general, it is perfectly fine to throw your own exceptions. Perhaps what you meant to ask was "When is it not necessarily a good idea to throw my own exception?"

    One case is when you should be throwing a standard exception. For example, if your method takes a file name and is supposed to return a file, you should probably throw your platform's standard FileNotFoundException rather than throw PeanutPowersFileNotFoundException. If you really want to throw your own exception, you should probably have it extend the standard FileNotFoundException.

    Update: Bloch explains this in Item 60 of Effective Java