Results 1 to 2 of 2
  1. #1
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,390
    Rep Power
    5

    Default Checked vs Unchecked Exceptions

    According to the JLS, unchecked exceptions (i.e subclasses of RuntimeException or Error) are exempt from compiler checking.
    So no matter what you do, you can't force, say, a NullPointerException to be flagged as a compile time error if its not in a
    try/catch block. To achieve this, I assume that the RuntimeException and Error classes are tightly coupled with the compiler,
    almost like keywords. Is the correct?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Checked vs Unchecked Exceptions

    Yes, they are coupled with the compiler in that the compiler handles them differently from checked exception. This is covered to a certain extent at Unchecked Exceptions — The Controversy (The Java™ Tutorials > Essential Classes > Exceptions).

    However this doesn't mean that you should never declare a method to throw a RuntimeException. Quoting from the article,
    One case where it is common practice to throw a RuntimeException is when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null. If an argument is null, the method might throw a NullPointerException, which is an unchecked exception.
    In other words, a method can throw a RuntimeException, and specify in its signature that it does so, e.g.,
    Java Code:
    /**
     * Example method that throws a RuntimeException
     * 
     * @param obj Object with which to do something
     * @throws NullPointerException if obj is null
     */
    void doSomethingWithObj(Object obj) throws NullPointerException {
        if (obj == null) {
            throw new NullPointerException("obj is null");
        }
        // do something
    }
    You can, of course, not specify NullPointerException in the throws clause, but specifying it, especially with accompanying javadoc comments, clearly communicates to users of this method of the effect of using the method incorrectly. I find this especially useful with an IDE when I can mouse over the method name when calling it from another class, and see the javadoc comment in situ. You would've encountered cases where a null parameter value is a valid value. In the above case, it clearly states that null is not valid. I can therefore check this during coding, and not to stumble across it during code execution.

Similar Threads

  1. Replies: 7
    Last Post: 08-23-2013, 11:25 AM
  2. Checked Exception
    By deeevo in forum New To Java
    Replies: 1
    Last Post: 06-13-2013, 09:58 AM
  3. why we need checked exceptions
    By akiravelmont in forum New To Java
    Replies: 2
    Last Post: 12-03-2011, 07:51 PM
  4. unchecked exceptions
    By veens4444 in forum New To Java
    Replies: 1
    Last Post: 06-08-2010, 06:36 AM
  5. Checked Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 06:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •