首页 > 编程语言 > 详细

[Java in NetBeans] Lesson 16. Exceptions.

时间:2019-01-01 10:24:23      阅读:172      评论:0      收藏:0      [点我收藏+]

这个课程的参考视频和图片来自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. 

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

 

[Java in NetBeans] Lesson 16. Exceptions.

原文:https://www.cnblogs.com/Johnsonxiong/p/10204118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!