简体   繁体   中英

Exception handling in Scanner

Is it necessary to handle exceptions when using the Scanner class in java. How can you explain about this?

I do not have correct idea about this. Any one can help me?

Any use of methods that throws exceptions should handle, rethrow, or redeclare (if it is a checked exception) them for the next level up to choose what to do with it.

In Scanner , if you take nextDouble() method, it throws various exceptions depending on the status of the scanner. You have to care about it.
But the way to care depend on your program logic.

That said, most exceptions in Scanner are RuntimeException , meaning you don't need to explicitly catch them in your code. Assuming you are in a main , if an exception occurs, it will be handled by at top level by the JVM. I this case, your program will exit with an error code and a stacktrace.

The only exceptions in Scanner which are checked and that you must handle in some way, is when you construct a Scanner from a files. IOException and FileNotFoundException cannot be ignored.


As an example of (abused) usage of exceptions when you read a double , you may be tempted to do:

public static void testScanDoubleWithException() {
    Scanner sin = new Scanner(System.in);
    System.out.println("Enter a double: ");
    while (true) {
        try {
            double d = sin.nextDouble(); // Throw if not a double
            System.out.println("Read double value: "+d);
            break;
        }
        catch (InputMismatchException e) {
            System.out.println("Invalid double, shoot again:");
            sin.nextLine(); // consume bad input
        }
    }
    sin.close();
}

But the Scanner class provides the more simple/explicit way:

public static void testScanDouble() {
    Scanner sin = new Scanner(System.in);
    System.out.println("Enter a double: ");
    while (!sin.hasNextDouble()) {
        System.out.println("Invalid double, shoot again:");
        sin.nextLine();
    }
    double d = sin.nextDouble();
    System.out.println("Read double value: "+d);
    sin.close();
}

Java supports exceptions handling so that we can avoid unwanted behaviors in our applications. During any transaction, if we foresee any unwanted behaviors we should handle exceptions. Scanner class can throw any exception based on the type of operation used, eg Runtime, illegalArgument, numberformat etc. thus as a good practice we should always catch all specific exceptions individually and have a better execution.

Yes. For example, if you are using a Scanner object to read a file, you should handle the FileNotFoundException otherwise there will be an error. However, if you are simply using System.in as input, no error will be displayed. But, at the same time, you should check if the input is according to what you wanted from the user, hence it is always advisable to make use of exception handling.

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