简体   繁体   English

如何在 Java 中捕获异常并继续处理?

[英]How to catch an exception and continue processing in Java?

So I have a program that receives two letters from the user and if those two letter do not begin with either U,C or P, it throws an error.所以我有一个程序接收来自用户的两个字母,如果这两个字母不以 U、C 或 P 开头,则会引发错误。

So far the code that I have created works just fine, now I would like to catch the exception but continue processing more data from the user.到目前为止,我创建的代码运行良好,现在我想捕获异常但继续处理来自用户的更多数据。

This is my code so far:到目前为止,这是我的代码:

import java.util.Scanner;

public class Class113 {

   public static void main(String[] args) {

       // Declare scanner
       Scanner scan = new Scanner(System.in);

       // Display a message to instruct the user
       System.out.println("Enter the designation: ");
       // Receive input
       String str = scan.next();

       try{
           // Verify str is not null and make sure UCP are capital letters whens ubmitted
           if (str != null && (str.charAt(0) == 'U' || str.charAt(0) == 'C' || str.charAt(0) == 'P') && str.length() == 2) {
               System.out.println("Valid designation");
           } else{
               throw new InvalidDocumentCodeException("Invalid designation. Designation must be two characters and start with U, C or P.");
           }
       } catch(InvalidDocumentCodeException invalidDocError){
           System.out.println(invalidDocError);
       }
   }
}

Just use a while loop.只需使用一个while循环。

while(true)

is effectively infinite, but you can get out of it with a break .实际上是无限的,但你可以通过break它。

I used "q" as the string to signal quitting the program, but you can use something else if you want.我使用"q"作为字符串来表示退出程序,但如果你愿意,你可以使用其他东西。

import java.util.Scanner;    

class InvalidDocumentCodeException extends RuntimeException {

    public InvalidDocumentCodeException(String s) {
        super(s);    
    }
}

public class Class113 {

    public static void main(String[] args) {
        // the try here is called try with resource
        // it will close the scanner at the end of the block.
        try(Scanner scan = new Scanner(System.in)){

            while(true){
                // Display a message to instruct the user
                System.out.println("Enter the designation: ");
                // Receive input
                String str = scan.next();

                if(str.equals("q")) break;

                try {
                    // Verify str is not null and make sure UCP are capital letters whens ubmitted
                    if (str.length() == 2 && (str.charAt(0) == 'U' || str.charAt(0) == 'C' || str.charAt(0) == 'P')) {
                        System.out.println("Valid designation");
                    } else{
                        throw new InvalidDocumentCodeException("Invalid designation. Designation must be two characters and start with U, C or P.");
                    }
                } catch(InvalidDocumentCodeException invalidDocError){
                    System.out.println(invalidDocError);
                }
            }
        }
    }
}

Unless you need to signal to other parts of your program about the error, I wouldn't use an exception here.除非您需要向程序的其他部分发出有关错误的信号,否则我不会在这里使用异常。 It would be enough to just print out in the else block.只需在 else 块中打印就足够了。

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

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