[Java in NetBeans] Lesson 16. Exceptions.

时间:2023-03-09 20:37:29
[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.

[Java in NetBeans] Lesson 16. Exceptions.

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.

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.");
}
}