views:

225

answers:

2

Hi all,

Imagine I have the methods:

public static void funcA() {...}

public static void funcB() 
{
    byteBuffer.wrap(someByteArray, 0, someByteArra.length);
}

IN JAVA API:

public static ByteBuffer wrap(byte[]array, int offset, int length)
{
    try {
        return new HeapByteBuffer(array, offset, length);
    } catch (IllegalArgumentException x) {
        throw new IndexOutOfBoundsException();
    }
}

function chain: funcB() -> ByteBuffer.wrap()

My question is how come funcB does not need to do a try-catch block around this java api method that throws an exception. funcB compiles fine without a try-catch block. I believe the answer has to do with the fact that the java api method throws an exception BUT IS NOT DECLARED as "throws IndexOutOfBoundsException"

function chain: funcA() -> funcB() -> ByteBuffer.wrap(...)

My next question is when I DO change funcB to "funcB() throws IndexOutOfBoundsException" how come funcA doesn't need to catch funcB's thrown exception? Does the compiler dig deep and realize that ByteBuffer.wrap(...) isn't declared as "wrap() throws IndexOutOfBoundsException" so all callers don't actually need to catch anything even sub-callers (funcB in this case) actually are declared as "funcB throws IndexOutOfBoundsException"?

Sorry if that was confusing or hard to understand.

Please help.

jbu

+9  A: 

IndexOutofBoundsException extends RuntimeException. Its a runtimeexception, doesn't need to be checked.

See unchecked exceptions - the controversy.

John Ellinwood
so what's the point of having throws clauses if there are exceptions that don't need to be caught?
jbu
Sometimes you want the OPTION of catching the exception without having to explicitly rethrow.
John Ellinwood
Ah, that would be a nice option. I can see it leading to laziness though.
jbu
The inverse is more common - checked exceptions that you simply cannot handle or don't care about more often than not get squeltched with an empty catch than actually logged or propagated - for instance, many times IOException on close is ignored for good reason, it should be a RuntimeException...
MetroidFan2002
IOException should not be a RuntimeException as it does not indicate a programmer mistake - http://stackoverflow.com/questions/588546/does-close-ever-throw-an-ioexception.
TofuBeer
An IOException on close probably means that there was an error outside of you control that you should warn the user about (or log if it's a webapp).
R. Bemrose
+4  A: 

At the top of the exception hierarchy is java.lang.Throwable. It is a checked exception (the compiler forces you to catch it or declare that you throw it).

Below Throwable there is Exception, also a checked exception, and Error, an unchecked exception (the compiler doesn't warn you about it).

Below Exception there is RuntimeException, also an unchecked exception.

The way that the designers of Java intended exceptions to be used is:

  • Exception, things that can go wrong
  • Error, low level things that can go wrong and a program cannot recover from
  • RuntimeException, programmer errors (like going past the end of an array, or calling a method on null).

The idea behind not having to catch unchecked exceptions is that they indicate failures (VM level, or programmer) that either you cannot handle (VM error) or should not exist in a properly debugged program (programmer mistake).

Not everyone agrees with the intent of the designers of Java on this and chose to use RuntimeException to mean things other than programmer mistake.

TofuBeer