简体   繁体   English

处理异常后返回程序中的特定位置

[英]Returning to the specific place in program after handling the exception

Good morning.早上好。

I suppose it's a very simple question for you guys...我想这对你们来说是一个非常简单的问题......

In below code, exception NumberFormatException, can be thrown in to places, when we provide value for "a" and "b" variables.在下面的代码中,当我们为“a”和“b”变量提供值时,异常 NumberFormatException 可以被抛出。 Catch block handles exceptions by starting the method again, no matter if the exception was trigged by wrong value of "a" or "b". Catch 块通过再次启动方法来处理异常,无论异常是由“a”还是“b”的错误值触发的。 I would like to change the code in a way that if the exception occurs while providing value for varaible "b", the method start not from the very beginning, but from the place where I'm suppose to provide value for "b" (in other words I don't want the user to go again from the start and provide value for "a" variable我想以某种方式更改代码,如果在为变量“b”提供值时发生异常,则该方法不是从一开始就开始,而是从我假设为“b”提供值的地方开始(换句话说,我不希望用户从一开始就再次 go 并为“a”变量提供值

Suppose I could insert two more methods handling the code where I provide the values for "a" and "b"... but is there any other way to get the same functionality without implementing new methods?假设我可以插入另外两个方法来处理我提供“a”和“b”值的代码......但是有没有其他方法可以在不实现新方法的情况下获得相同的功能?

import java.io.*;

public class Rozdzial1{
    
    public static void Zadanie11()
    throws IOException
    {
        try {
        Double a, b, area;
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader read = new BufferedReader(input);
        System.out.println("The program calcultes area of rectangle");
        System.out.println("Provide length of first edge: ");
        a = Double.parseDouble(read.readLine());
        System.out.println("Length of the first edge is: " + a);
        System.out.println("Provide length of second edge: ");
        b = Double.parseDouble(read.readLine());    
        System.out.println("Length of the second edge is: " + b);   
        
        area = a*b;
        
        System.out.println("Area of provided rectangle is: " + area);
        }
        
        catch (NumberFormatException exception) {
            System.out.println("Provided vale should be a number, please try again\n");
            Rozdzial1.Zadanie11();
        }
    }

    public static void main(String[] args) 
    throws IOException
    {
        Rozdzial1.Zadanie11();
    }
    
}

Reading values until valid value is entered should be extracted into a separate method, which should be called for a and b variables separately.在输入有效值之前读取值应提取到单独的方法中,该方法应分别为ab变量调用。

public static void main(String[] args) {
    Task1();
}

public static void Task1() {
    Scanner scan = new Scanner(System.in);

    System.out.println("The program calculates area of rectangle");

    double a = readValue(scan, "first edge");
    double b = readValue(scan, "second edge");
    System.out.println("Area of provided rectangle is: " + (a * b));

}

private static double readValue(Scanner scan, String name) {
    try {
        System.out.println("Provide length of " + name + ": ");
        return Double.parseDouble(scan.nextLine()); // exit from recursion with valid value
    } catch (NumberFormatException numex) {
        System.out.println("Provided value should be a number, please try again");
        return readValue(scan, name); // recursive call for invalid value
    }
}

Another option (without creating a separate method) could be to use nested loops to read inputs until valid value is provided:另一种选择(不创建单独的方法)可能是使用嵌套循环来读取输入,直到提供有效值:

public static void Task2() {
    Scanner scan = new Scanner(System.in);

    System.out.println("The program calculates area of rectangle");

    String[] names = {"first edge", "second edge"};
    double[] arr = new double[2];

    for (int i = 0; i < arr.length; i++) {
        try {
            System.out.println("Provide length of " + names[i] + ": ");
            arr[i] = Double.parseDouble(scan.nextLine());
        } catch (NumberFormatException numex) {
            System.out.println("Provided value should be a number, please try again");
            i--; // repeat the input
        }
    }

    System.out.println("Area of provided rectangle is: " + (arr[0] * arr[1]));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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