[Java in NetBeans] Lesson 16. Exceptions.

这个课程的参考视频和图片来自youtube

    主要学到的知识点有:

We want to handle the bad Error. (e.g bad input / bugs in program)

  • Error: a type of Exception  

    e.g   File I/O;   User Input, out of control.

 An example that handle the wrong input and out of range input. 

1. First we creat two java class (IntegerOutOfRangeException and InputMisMatchException)

public class IntegerOutOfRangeException extends Exception {
}
public class InputMisMatchException extends Exception {
}

2. Then we will use try and  catch to catch the exceptions. 

扫描二维码关注公众号,回复: 4731103 查看本文章
try {
    i = input.nextint();
    if (i < 1 || i > 10) {
        throw new integerOutOfRangeException();
    }
    catch (inputMisMatchException ex){
        System.out.println("you did not enter an integer.");
    }
    catch (integerOutOfRangeException ex){
        System.out.println("your input value is out of range.");
    }
}

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/10204118.html