如何在代码的这一部分添加异常处理?

时间:2021-02-20 07:16:08

I have two problems. First, I have created an exception handling in this first part of my code, the try again concept.

我有两个问题。首先,我在代码的第一部分创建了一个异常处理,再试一次概念。

do {
    try {
        if(exitType.length()==1){
            char exitChar = exitType.charAt(0);
            exit = exitChar;
            if (exit == 'Y' || exit == 'y' || exit == 'N' || exit == 'n') {
                x = 1; 
            } else {
                throw new StringException("Invalid letter...\n");
            }
        } else {
            throw new StringException("Invalid input a string...\n");
        } 
    } catch(StringException i) {
        System.out.println("-------------------------------------------");
        System.out.print("You typed: " + exitType + i);
        System.out.println("-------------------------------------------");

        System.out.println("Try again? (y/n): ");
        exitType = input.next();
        x = 0;
    }

The output when the user enters a letter besides y/n, would be:

当用户输入除y / n之外的字母时的输出将是:

Try again? (y/n): w
-------------------------------------------
You typed: wStringException: Invalid letter...
-------------------------------------------

First question: How do I put the StringException: Invalid letter... on the next line so it's not beside the 'w' (just for clarity and neatness of output). Hope you get me.

第一个问题:如何在下一行放置StringException:无效的字母...所以它不在'w'旁边(只是为了清晰和输出的整洁)。希望你能得到我。

By the way, I have created my own exception:

顺便说一下,我创建了自己的例外:

public class StringException extends Exception {
    public StringException(String message) {
        super(message);
    }   
}

Second, I cannot figure out how to add exception handling in this part where the user is asked to enter the letter of choice:

其次,我无法弄清楚如何在要求用户输入选择字母的部分中添加异常处理:

public static void operation() {
    Scanner input = new Scanner(System.in);
    String choiceString = "";
    char choice = 'a';
    System.out.print("Enter letter of choice: ");
    choiceString = input.next();
    if (choiceString.length() == 1) {
        choice = choiceString.charAt(0);
        System.out.println("-------------------------------------------");

        switch(choice) {
            case 'a': {
                try {
                    System.out.print("Enter width: ");
                    double width = input.nextDouble();
                    System.out.print("Enter height: ");
                    double height = input.nextDouble();
                    System.out.print("What is the color of the shape? ");
                    String color = input.next();
                    System.out.println("-------------------------------------------");
                    Shape cia;
                    Shape rec = new Rectangle(color, width, height);
                    cia = rec;
                    System.out.println(rec);
                    Rectangle r = new Rectangle(color, width, height);
                    r.print();
                } catch(InputMismatchException i) {
                    System.out.println("InputMismatchException caught");
                }
                break;  
            }
            case 'b': {
            //**** 
            }
            case 'c': {
            //****
            }
            default:
                System.out.println("Invalid choice...");
        }
    } else {
        System.out.println("Invalid input...");
    }

I only need to know where I should place the try-catch block only on the part where it prompts about the letter of choice.

我只需知道我应该将try-catch块放在哪里,只是在它提示选择字母的部分。

2 个解决方案

#1


0  

First question: How do I put the StringException: Invalid letter... on the next line so it's not beside the 'w'

第一个问题:如何在下一行放置StringException:无效的字母...所以它不在'w'旁边

Change

System.out.println("You typed: " + exitType);
System.out.println(" ");
System.out.println(i);

to

System.out.println("You typed: " + exitType + i);

println adds a newline after printing what is in the argument (which is different from print)

println在打印参数中的内容后添加换行符(与print不同)

Second, I cannot figure out how to add exception handling in this part where the user is asked to enter the letter of choice:

其次,我无法弄清楚如何在要求用户输入选择字母的部分中添加异常处理:

You can add it in the default case or, better, in the else

您可以在默认情况下添加它,或者更好地在其他情况下添加它

else {
        throw new ...
    }

#2


0  

First question: in your catch you have

第一个问题:在你的捕获中你有

System.out.print("You typed: " + exitType + i);

just change to

只是换到

System.out.print("You typed: " + exitType + "\n" + i);

\n is an escaping code which acts as a new line. For sake of completeness, you should actually use \n\r for system compatibility

\ n是一个转义代码,用作新行。为了完整起见,您实际上应该使用\ n \ r来获得系统兼容性

I didn't really get the second question, isn't it enough if you place your try/catch around choiceString = input.next();? It also depends how you want to manage the exception after you catch it.

我没有真正得到第二个问题,如果你把try / catch放在choiceString = input.next();?还不够吗?它还取决于您在捕获异常后如何管理异常。

#1


0  

First question: How do I put the StringException: Invalid letter... on the next line so it's not beside the 'w'

第一个问题:如何在下一行放置StringException:无效的字母...所以它不在'w'旁边

Change

System.out.println("You typed: " + exitType);
System.out.println(" ");
System.out.println(i);

to

System.out.println("You typed: " + exitType + i);

println adds a newline after printing what is in the argument (which is different from print)

println在打印参数中的内容后添加换行符(与print不同)

Second, I cannot figure out how to add exception handling in this part where the user is asked to enter the letter of choice:

其次,我无法弄清楚如何在要求用户输入选择字母的部分中添加异常处理:

You can add it in the default case or, better, in the else

您可以在默认情况下添加它,或者更好地在其他情况下添加它

else {
        throw new ...
    }

#2


0  

First question: in your catch you have

第一个问题:在你的捕获中你有

System.out.print("You typed: " + exitType + i);

just change to

只是换到

System.out.print("You typed: " + exitType + "\n" + i);

\n is an escaping code which acts as a new line. For sake of completeness, you should actually use \n\r for system compatibility

\ n是一个转义代码,用作新行。为了完整起见,您实际上应该使用\ n \ r来获得系统兼容性

I didn't really get the second question, isn't it enough if you place your try/catch around choiceString = input.next();? It also depends how you want to manage the exception after you catch it.

我没有真正得到第二个问题,如果你把try / catch放在choiceString = input.next();?还不够吗?它还取决于您在捕获异常后如何管理异常。