码迷,mamicode.com
首页 > 其他好文 > 详细

2019.2.20异常

时间:2019-02-20 14:40:01      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:++   nbsp   etag   void   row   imp   rac   this   pac   

1.

技术图片

 

package qq;

import java.util.Scanner;

public class KeCheng {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        try {
            System.out.println("请输入1~3之间的数字:");
            int choose = input.nextInt();
            switch (choose) {
            case 1:
                System.out.println("JAVA");
                break;
            case 2:
                System.out.println("C++");
                break;
            case 3:
                System.out.println(".net");
                break;
            default:
                System.out.println("输入错误!没有该课程");
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("出现异常!");
        }finally {
            System.out.println("感谢建议");
        }
        
    }
}

输出为:

技术图片

 

2.

技术图片

 

package qq;
/**
 * 年龄类
 */
public class Age {
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws Exception{
        if(age<100&&age>0) {
            this.age = age;
        }else {
            throw new Exception("年龄不能小于0或者大于100");
        }
    }
    public void print() {
        System.out.println("XXX的年龄为:"+this.getAge());
    }
    
}

package qq;
/**
 * 年龄测试类
 */
import java.util.Scanner;

public class AgeTest {
 public static void main(String[] args){
  Scanner input = new Scanner(System.in);
  try {
   System.out.println("请输入年龄:");
   Age age = new Age();
   age.setAge(input.nextInt());
   age.print();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
}

 输出为:

技术图片

 

技术图片

先执行finally块,在执行return

System.exit(1);退出虚拟机

2019.2.20异常

标签:++   nbsp   etag   void   row   imp   rac   this   pac   

原文地址:https://www.cnblogs.com/Zhangchuanfeng1/p/10406380.html

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