Women in Technology

Hear us Roar



Article:
  Best Practices for Exception Handling
Subject:   declaring impossible exceptions
Date:   2003-11-21 09:19:42
From:   anonymous2
I think it's important that Java allows this:


public void someMethod() throws Exception{
}


OK, so no method should "throws Exception", but if that were "throws UserNotFoundException", I think that would be just fine.


Keep in mind that the throws clause is part of the method signature, and thus part of the API. You might want to do this if you think that a future version of your library might throw the exception and want your clients to have an easy transition. Or if you think subclasses that override the method will need to throw that exception, you have to declare it in the base class--even if the base class implementation never actually throws it.

Full Threads Newest First

Showing messages 1 through 1 of 1.

  • declaring impossible exceptions
    2003-11-21 16:58:56  anonymous2 [View]

    And what is proberly more important, is that by extending Exception (or some other exception) you create a new type that can be differentiated from other Exceptions and thereby giving the catching party an oppotunity to act differently on different types, without having to resort to string comparisons on the error message.