集册 Java实例教程 捕获多个异常类型以改善类型处理

捕获多个异常类型以改善类型处理

欢马劈雪     最近更新时间:2020-01-02 10:19:05

201
捕获多个异常类型以改善类型处理

import java.io.IOException;

import java.net.URI;
/**
n o w j a v a . c o m - 时代Java
**/

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.InputMismatchException;

import java.util.NoSuchElementException;

import java.util.Scanner;

import java.util.logging.Level;

import java.util.logging.Logger;


public class Main {


    private static final Logger logger = Logger.getLogger("log.txt");


    public static void main(String[] args) {

        System.out.print("Enter a number: ");

        try {

            Scanner scanner = new Scanner(System.in);
            /**
            nowjava.com - 时代Java 提供 
            **/

            int number = scanner.nextInt();

            if (number < 0) {

                throw new InvalidParameter();

            }

            System.out.println("The number is: " + number);

            if(number>10) {

                throw new AssertionError("Number was too big",new Throwable("Throwable assertion message"));

            }

        } catch (InputMismatchException | InvalidParameter e) {

            logger.log(Level.INFO, "Invalid input, try again");

            e.addSuppressed(new Throwable());

            System.out.println("Invalid input, try again");

        } catch (final Exception e) {

            logger.log(Level.INF
展开阅读全文