码迷,mamicode.com
首页 > 编程语言 > 详细

[Java in NetBeans] Lesson 16. Exceptions.

时间:2019-01-01 11:05:27      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:int()   png   extends   try   hex   java   div   分享图片   idt   

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

标签:int()   png   extends   try   hex   java   div   分享图片   idt   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!