简体   繁体   中英

multiple statements in try/catch block - Java

Im a bit uncertain as to whether this:

    try{
        worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
        }
        catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, "You have not entered a valid number");
        }

    try{
        worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
        }
        catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, "You have not entered a valid number");
        }

would do the same thing as:

    try{
        worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
        worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
        }
        catch (NumberFormatException e){
            JOptionPane.showMessageDialog(null, "You have not entered a valid number");
        }

basically want the user to enter a number, if it isnt a number exception gets thrown and the user gets re-asked for a number?

Thanks

In your first example, with the two separate try...catch blocks, it seems that when an exception is thrown, you are just showing a dialog, not stopping the flow of control.

As a result, if there is an exception in the first try...catch , control will continue to the second try...catch and the user will be asked to enter the second number, regardless of the fact that she did not enter the first number correctly.

In the second example, if there is an exception in the first try...catch , the user will not be presented with the second question, because control will not continue inside the try block, but rather after the catch block's end.

Yes this will work (almost) the same. In a try catch block it will only stop execution at the point where the error occurs. If it throws an error at the first line the second line will never be executed in the second option. That is the only difference, in the first option, the second line (input) will be executed no matter if the first line (input) throws an error or not.

You can also try following code

{
    int arr[]=new int[5];
    try
    {

        try
        {
            System.out.println("Divide 1");
            int b=23/0;
        }
        catch(ArithmeticException e)
        {
            System.out.println(e);
        }
        try
        {
            arr[7]=10;
            int c=22/0;
            System.out.println("Divide 2 : "+c);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Err:Divide by 0");
        }
        catch(ArrayIndexOutOfBoundsException e) 
    //ignored 
        {
            System.out.println("Err:Array out of bound");
        }
    }
    catch(Exception e)
    {
        System.out.println("Handled");
    }
}

For more detail visit: Multiple try-catch in java with example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM