Where to Place Try/Catch Statement in Postfix Expression Evaluation?

  • Thread starter Thread starter muna580
  • Start date Start date
  • Tags Tags
    Confusion
Click For Summary

Discussion Overview

The discussion revolves around the placement of try/catch statements in a method for evaluating postfix expressions using stacks. Participants explore error handling related to parsing integers and managing stack operations, with a focus on potential exceptions that may arise during evaluation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests placing the try/catch around the Integer.parseInt() call to handle NumberFormatException for non-integer strings.
  • Another participant argues that the try/catch should address malformed sequences like '2+', implying that such cases could lead to errors during evaluation.
  • A later reply acknowledges the existing try/catch structure but points out that the return statement could still throw an error if the stack is empty.
  • One participant reflects that malformed sequences would lead to an empty stack, indicating a misunderstanding of the stack's role in the evaluation process.

Areas of Agreement / Disagreement

Participants express differing views on where to place the try/catch statement and what exceptions should be caught, indicating that there is no consensus on the best approach to error handling in this context.

Contextual Notes

Participants discuss various potential exceptions without resolving the implications of malformed sequences or the specific conditions under which the stack may become empty.

muna580
I am evaluating a postfix expression using stacks and stuff. Below is the method I made. But I am not sure where to put the try/catch satement and what is it that i have to catch.

Code:
	public int evaluateRPN()
	{
		StringTokenizer token = StringTokenizer(postfix);
		stk.clear();
		
		try
		{
			while(token.hasMoreTokens())
			{
				String s = token.nextToken();
				
				if(isNumber(s))
					stk.push(new Integer(Integer.parseInt(s)));
				else
				{
					char operator = s.charAt(0);
					int x1 = ((Integer)stk.pop()).valueOf();
					int x2 = ((Integer)stk.pop()).valueOf();
					
					int x3; 
					
					switch(operator)
					{
						case StackConstants.ADDITION_OPERATOR:
							x3 = x2 + x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.SUBTRACTION_OPERATOR:
							x3 = x2 - x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.MULTIPLICATION_OPERATOR:
							x3 = x2 * x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.DIVISION_OPERATOR:
							x3 = x2 / x1;
							stk.push(new Integer(x3));
						break; 
											
						case StackConstants.MODULUS_OPERATOR:
							x3 = x2 % x1;
							stk.push(new Integer(x3));
						break; 
					}
				}
			}
		}
		catch(EmptyStackException e)
		{
			
		}
		
		return ((Integer)stk.pop()).valueOf(); 
	}
 
Technology news on Phys.org
You should put the try/catch around the Integer.parseInt() call. It will throw a NumberFormatException if the string you are trying to parse into an integer is not an integer, like the string "abc."
 
I don't think so, that parseInt() is only when isNumber(). I think the try/catch should catch malformed sequences like '2+' (and I'm not saying where that is because I imagine this is homework).
 
Oh, silly me, there is already a try/catch around almost the entire thing. The only place left is the return statement (which will error on empty stack). Stupid question. Although, if you want to catch malformed sequences you can still do that.
 
Last edited:
Having thought about this more, I realize that malformed sequences will always result in an empty stack because the stack only ever contains numbers. I had in mind a stack containing the complete expression, whereas this one only contains intermediate results.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K