Jump to content
  • Advertisement
Sign in to follow this  
plywood

Java exceptions example with throws clause

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get the hang of Java exceptions, and think I have the gist of them down - save for the throws clause. Observe the following code:
public class Widget
{
    // ... stuff, etc.

    public static void main(String[] args)
    {
        // whatever...

        try
        {
            DoSomething();
            DoSomethingElse();
        }
        catch(IOException ioExc)
        {
            // Handle...
        }
        catch(ArrayOutOfBoundsException aoobExc)
        {
            // Handle...
        }
        catch(Exception exc)
        {
            // Handle...
        }
        finally
        {
            // Close things etc to prevent leakage.
        }
    }

    private String DoSomething() throws IOException
    {
        if(!foo)
            throw new IOException();

        DoSomethingElse();
    }

    private void DoSomethingElse() throws ArrayOutOfBoundsException
    {
        if(someValue == 5)
            throw new ArrayOutOfBoundException();
    }
}

What I don't understand is this: in the main method we call DoSomething() and DoSomething() else; if an exception occurs it bubbles up one level and is immediately caught-and-handled. But then DoSomething() also calls DoSomethingElse() inside it, but is marked to only catch IOExceptions. (1) So if that DoSomethingElse() - the one nested inside of DoSomething() - throws an ArrayOutOfBoundsException, will it "make it" to the exception handler up in main? Or does the throws IOException clause in DoSomething filter out the array exception? (1b) If that's the case, then I would assume I would need to mark DoSomething() as also throwing array exceptions in order for those types of exceptions to bubble up, yes? (2) Also, if no throws clause is added to a method definition, and a specific exception (IOException, WuTangException, etc.) occurs inside of it, can it throw? Or does not marking a method with "throws X" prevent it from being throwable? I'm so confused.

Share this post


Link to post
Share on other sites
Advertisement
ArrayIndexOutOfBoundsException is a RuntimeException and so it doesn't need to be caught, so it does get lost in DoSomething() method; if you want to catch it then your DoSomething method should be


private String DoSomething() throws IOException, ArrayIndexOutOfBoundsException {
if(!foo)
throw new IOException();

DoSomethingElse();
}



or handle the exception in DoSomething()


private String DoSomething() throws IOException {
if(!foo)
throw new IOException();
try {
DoSomethingElse();
} catch (ArrayIndexOutOfBoundsException ex) {
//handle the exception
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by plywood
(1) So if that DoSomethingElse() - the one nested inside of DoSomething() - throws an ArrayOutOfBoundsException, will it "make it" to the exception handler up in main?

Yes. Exceptions cause the stack to unwind until it is caught or the stack is empty. If you aren't sure what stack unwinding is, I suggest you read up on the call stack.

Quote:
Original post by plywood
Or does the throws IOException clause in DoSomething filter out the array exception?

The throws clause just means "I don't handle this exception and I might throw it, so you have to be prepared to handle it." It's possible for a function to throw exceptions that it doesn't declare in its throws declaration.

Quote:
Original post by plywood
(1b) If that's the case, then I would assume I would need to mark DoSomething() as also throwing array exceptions in order for those types of exceptions to bubble up, yes?

No, because that's not the case.

Quote:
Original post by plywood
(2) Also, if no throws clause is added to a method definition, and a specific exception (IOException, WuTangException, etc.) occurs inside of it, can it throw? Or does not marking a method with "throws X" prevent it from being throwable? I'm so confused.

Maybe. There are checked and unchecked exceptions in Java (feel free to google for more info). If your function throws an unchecked exception but does not say so in its throws clause, you will get a compile time error. So you can't throw an unchecked exception without saying you might in your throws clause. However, you can throw an unchecked exception without necessarily saying you will.

[edit]

ninja'd++;

Share this post


Link to post
Share on other sites
Quote:
Original post by plywood
What I don't understand is this: in the main method we call DoSomething() and DoSomething() else; if an exception occurs it bubbles up one level and is immediately caught-and-handled.

But then DoSomething() also calls DoSomethingElse() inside it, but is marked to only catch IOExceptions.


It is only marked to throw IOExceptions. Any method may potentially catch anything.

Quote:
(1) So if that DoSomethingElse() - the one nested inside of DoSomething() - throws an ArrayOutOfBoundsException, will it "make it" to the exception handler up in main?


Yes. "throws" clauses are not filters; they are advertisements.

ArrayIndexOutOfBoundsException does not need to be advertised, because it derives from RuntimeException, and is thus not a "checked" exception. The Java language considers that IOExceptions are important enough that if you don't handle them locally, you should be forced to advertise the fact. (You aren't, IIRC, prohibited from advertising non-checked exceptions; but it doesn't really do anything.)

Quote:
(2) Also, if no throws clause is added to a method definition, and a specific exception (IOException, WuTangException, etc.) occurs inside of it, can it throw? Or does not marking a method with "throws X" prevent it from being throwable? I'm so confused.


If the exception is of a checked type, the compiler will flag an error, and tell you, in effect: "either add the throws() clause or catch the exception". If it is of an unchecked type, the code compiles and the exception may be thrown.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!