How to come to the top of the method when Exception occurs

Program stotp at the end of the catch block, I want to come to the top

Page 1 of 1

1 Replies - 470 Views - Last Post: 25 November 2009 - 09:20 PM Rate Topic: -----

#1 itsmeisuru  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-November 09

How to come to the top of the method when Exception occurs

Post icon  Posted 25 November 2009 - 08:29 PM

I have used ' > ' symbol for the user to find where he is at, bt when an exception occurs the code doesnt prompt ' > ' symbol, to get it, user have to
press enter.

public void readComand(){
		boolean exit = false;	//variable to stop the following while loop
		do{
			boolean isCont = true;
			do{
				System.out.print("\n>");	//prompt to the user
				try{
					
					String cm = inputS.nextLine();
					String cmnd[] = cm.split(" \\s*");	//split the inputs at spaces, typed by the user and assign them to the cmnd array
					
					if(cmnd[0].equals("update")|cmnd[0].equals("sum")|cmnd[0].equals("add_rule")|cmnd[0].equals("average")|cmnd[0].equals("fill")|cmnd[0].equals("median")|cmnd[0].equals("sum-set")|cmnd[0].equals("search")|cmnd[0].equals("sort")|cmnd[0].equals("show")|cmnd[0].equals("exit")){
					}
					else{
						System.out.println("Error: Invalid Command.\n If you want to quit use 'exit' command.");
					}
					
					if(cmnd[0].equals("update")){
						update(cmnd[1]);
					}
					
					if(cmnd[0].equals("show")){	//get activated when user inputs show
						gridShow();	//this calls the gridShow method
					}
					
					if(cmnd[0].equals("exit")){
						System.exit(0);
					}
					
					if(cmnd[0].equals("fill")){	//fill				
						fillArray();	//this also called the above method fillGrid()
					}
					
					if(cmnd[0].equals("sum")){	//getting sum
						sum(cmnd[1]);
					}
					
					if(cmnd[0].equals("average")){
						average(cmnd[1]);
					}
					
					if(cmnd[0].equals("median")){		//getting the median value
						median(cmnd[1]);
					}
					
					if(cmnd[0].equals("sum-set")){		//getting sum and set to a given point
						sumSet(cmnd[1]);
					}
					
					if(cmnd[0].equals("search")){	//search a given element 
						search(cmnd[1]);
					}
					
					if(cmnd[0].equals("sort")){		//sorting the values in given points
						sort(cmnd[1]);
					}
					
					if(cmnd[0].equals("add_rule")){
						addRule(cmnd[1]);
					}
					isCont = false;
				}
				
				catch(Exception e){
					System.err.println("Error: "+e);
					System.out.println("Try again...");
					inputS.nextLine();
				}
				
				
			}while(isCont);
					
		}while(exit==false);
	}




Is This A Good Question/Topic? 0
  • +

Replies To: How to come to the top of the method when Exception occurs

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: How to come to the top of the method when Exception occurs

Posted 25 November 2009 - 09:20 PM

that is why try/cath has been invented to be catch at a highest level

boolean exception = true;
while(exception) {
   try {
	  readCommand();
	  exception = false;
  }
  catch(Exception e) {
  }
}



an remove the try/catch from your readCommand() method
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1