简体   繁体   中英

try/catch not working for static member throwable class?

I am having some issues with throwing static member classes, as the title suggests. But I'm not sure if that's the problem, and the compiler gives me mixed signals as to what I should be doing! My program's classes and functions are all in a single file.

Here's the definition of one of my Throwables (the others are essentially the same):

private static class ParenthesisException extends Throwable
{
    public ParenthesisException(){}
    public String strErrMsg()
    {
        return "ERROR: Every '(' needs a matching ')'";
    }
}

I'm throwing this in a member function that gets called by main inside a try/catch, statement. I throw my other Throwable class inside a function being called by said function being called by main in the try/catch statement. Here's a little segment of it:

try
{
    parseFormula(cin.nextLine());
}
catch(ParenthesisException e)
{
    System.out.println(e.strErrMsg());
}
catch(OperatorException e)
{
    System.out.println(e.strErrMsg());
}

parseFormula is the static member function throwing ParenthesisException .

The compiler says:

Unreachable catch block for jcalc.ParenthesisException. This exception is never thrown from the try statement body

but I did throw it, here, in parseFormula:

if(n_lpar != n_rpar)
    throw new ParenthesisException();

Oddly enough, when I remove the try/catch, it tells me that the exception isn't caught. This confuses me, because before, it said that ParenthesisException was never thrown, and now it says it is never caught, but is thrown.

I'm wondering if this is related to the way they are all static members(the classes and functions), and how I can resolve this.

Your mistake is that your ParenthesisException extends Throwable directly.

You should not do this. Extend Excption if you wish your exception to be checked exception (that compiler forces you to catch it), RuntimeException if you do not want compiler to force you or Error if you think that this is something fatal.

You are not declaring that parseFormula() throws an exception.

When you declare parseFormula() you need to add throws ParenthesisException .

try { 
    parseFormula(cin.nextLine()); //This doesn't actually throw anything
} catch(ParenthesisException e) { 
    System.out.println(e.strErrMsg()); 
} catch(OperatorException e) { 
    System.out.println(e.strErrMsg()); 
}

parseFormula needs to be declared with throws ParenthesisException, OperatorException so that when you call it in the try it knows that the method can throw exceptions, otherwise you are trying to do something it thinks doesn't throw any exceptions and therefore complains saying that you are never throwing any exceptions in the try block and because of that there is nothing to catch.

EDIT: [I must be a slow typer, didn't see the two new answers but yes I agree with what J Lundberg said]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM