tags:

views:

90

answers:

4
/**
 * An exception thrown when an illegal side pit was
 * specified (i.e. not in the range 1-6) for a move
 */
public class IllegalSidePitNumException extends RuntimeException
{
    /**
     *  Exception constructor.
     *  @param sidePitNum the illegal side pit that was selected.
     */
    public IllegalSidePitNumException(int sidePitNum)
    {
        super("No such side pit number: "+sidePitNum);
    }
}

How do I use this in a program and then resume for there? I do not want the program to end but want to handle the exception and continue.

+2  A: 

Just catch it.

try {
    doStuffWhichPossiblyThrowsThisException();
} catch (IllegalSidePitNumException e) {
    // Don't rethrow it. I would however log it.
}
continueWithOtherStuff();
BalusC
when an expertion of this type is thrown IllegalSidePitNumException. i have to catch it and then display meesage and then continue
fari
Then add a line which displays the message and waits until the user clicks OK or so. How to do this exactly depends on the UI technology you're using. I bet that it's Swing. If true, then put `JOptionPane.showMessageDialog(null, "Some message");` in `catch` block.
BalusC
+3  A: 

You need to use try/catch. You can learn a lot about exception handling from Sun's (Oracle's) exception handling tutorials. In that tutorial look at the sections about Catching and Handling that specific address your question.

For example, in the code that calls the method that may throw this exception:

try {
    ...call method here...
} catch (IllegalSidePitNumException e) {
    // Display message (per your comment to BalusC)
    System.err.println(e.getMessage());
    e.printStackTrace(System.err);

    // You can also handle the exception other ways (but do not ignore it)
    // Such as correcting some offending values, setting up for a retry
    // logging the information, throwing a different exception
}

...program continues executing here...
Kevin Brock
+1  A: 

Sidenote: If this is an "expected" exception, you might want to inherit it from Exception instead of RuntimeException. RuntimeExceptions are intended to be used when something unexpected happens, for example illegal input due to a programmer error.

Of course, you don't give the context where you intend to use the exception so this is all theory (but you do mention that you want to continue the execution).

fish
+1  A: 

As others have said, you can do the following:

try {
    doSomething();
} catch (SomeException ex) {
    doRecovery();
} 
doSomethingElse();

But there is no way to do something like the following in Java:

doSomething();
throw new SomeException(...);
doSomethingElse();  // ... after some handler has "resumed" the exception.

The above is a (hypothetical) example of "resumption model" exception handling, and Java does not support this. (Nor does C#, C++ or any other currently popular programming language ... though a couple of historical languages did support it.) In fact, the Java compiler will give you a compilation error for the above, saying that the statements after the throw are unreachable.

Stephen C