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

  • Thread starter muna580
  • Start date
  • Tags
    Confusion
In summary, the code is evaluating a postfix expression using stacks and the method includes a try/catch statement to catch any errors. The only place where a try/catch may be needed is around the return statement in case of an empty stack. The code also includes a switch case for handling different arithmetic operators.
  • #1
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
  • #2
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."
 
  • #3
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).
 
  • #4
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:
  • #5
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.
 

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

What is "Try and Catch Confusion"?

"Try and Catch Confusion" refers to a common programming error that occurs when using the try and catch statements in coding.

How does "Try and Catch Confusion" occur?

"Try and Catch Confusion" occurs when the try statement is used to catch an error, but the catch statement has not been properly defined. This can result in the error being uncaught and causing issues in the code.

What are the consequences of "Try and Catch Confusion"?

"Try and Catch Confusion" can lead to unexpected errors and bugs in the code, making it difficult to identify and fix issues. It can also result in the program crashing or not functioning as intended.

How can "Try and Catch Confusion" be prevented?

"Try and Catch Confusion" can be prevented by ensuring that the catch statement is properly defined and can handle the specific error that may occur. It is also important to thoroughly test the code to identify and fix any potential issues.

What are some tips for avoiding "Try and Catch Confusion"?

Some tips for avoiding "Try and Catch Confusion" include using specific catch statements for different types of errors, making sure to handle all possible errors, and using proper coding practices such as commenting and debugging to identify and fix any issues.

Similar threads

Replies
9
Views
4K
Replies
16
Views
3K
Replies
1
Views
3K
Replies
7
Views
2K
Back
Top