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.
You are not declaring that
parseFormula()throws an exception.When you declare
parseFormula()you need to addthrows ParenthesisException.