Transform the Expression....Run time error ....help me

import java.util.*;

class fund {

public static void main(String args[])
{
int cases,length,top=0;

char exp[]=new char[399];
char stack[]=new char[200];
Scanner sc=new Scanner(System.in);

cases=sc.nextInt();

while(cases>0)
{
	exp=sc.next().toCharArray();
	length=exp.length;
	for(int i=0;i<=length-1;i++)
	{ 
		if(exp[i]== '('  )
		continue;
		
		if(exp[i]== ')' )
		{
		  System.out.print(stack[top]);	
		  top--;
		}
		else if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/' || exp[i]=='^')
		{
			top++;
			stack[top]=exp[i];
		}
		else
		{
			System.out.print(exp[i]);
		}
	}
	
}

}
}

I think the following code segment gives the error:

if(exp[i]== ')' )
        {
          System.out.print(stack[top]); 
          top--;
        }

There might be cases where the above if statement is entered even when the stack is empty. Try putting a condition to check whether the stack is empty or not.