java - How to correctly throw an exception -
i'm new java , i'm trying account possible errors might come in program. i'm creating calculator , converting inputs infix postfix calculate result. want try accounting mismatched parenthesis in input, i'm having trouble. example, when converting infix postfix, when ) reached, pop numbers stack , put them onto new postfix list. in 1 case there may not matching left parenthesis present (the while loop reaches end of stack without coming across (, should throw exception. i've implemented following code, doesn't seem working:
else if(tok.equals(")")){ while(stack.peek().equals("(") == false){ try{ operator o = stack.pop(); nlist.add(o); } catch(emptystackexception e){ system.out.println(e.getmessage()); } stack.pop(); } then in other file constructs gui , processes inputs, entered:
try{ java.util.list<token> postfix = expressionmanager.infixtopostfix(infix); // build expression expression exp = expressionmanager.buildexpression(postfix); // display results }catch(arithmeticexception e){ entryfield.settext(e.getmessage()) } any suggestions?
stack.peek() throws emptystackexception before enter inside try-catch block (i assume expect pop throw exception).
second block doesn't show how arithmeticexception thrown, so, not sure expecting here.
Comments
Post a Comment