zoukankan      html  css  js  c++  java
  • java异常的捕获和处理

    system.exit(输入一个非零的数);
    表示程序退出。
    system.err.println
    表示输出的是红色的字体

    java中所有的异常都是Exception(父类)
    除数为0的异常,ArithmeticException(子类)
    输入格式不正确,InputMismathException(子类)

    作业:

    1、

    package com.yichang;
    
    import java.util.Scanner;
    
    public class LianXi {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入课程代号(1~3之间的数):");
            
            try {
                int daihao = 0;
            daihao = sc.nextInt();
            
                if(daihao == 1) {
                    System.out.println("c#编程");
                }else if(daihao == 2) {
                    System.out.println("java编程");
                }else if(daihao == 3) {
                    System.out.println("php编程");
                }else {
                    System.err.println("输入错误!");
                }
            } catch (Exception e) {
                System.err.println("输入错误!");
            }finally {
                System.out.println("欢迎提出建议!");
            }
            
        }
    }

     

    2、

    package com.yichang;
    
    public class Person {
    
        private String name;
        private String sex;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) throws Exception{
            if(age>1 && age<100) {
            this.age = age;
            }else {
                throw new Exception("年龄必须在1到100之间");
            }
        }
        
        public void print() {
            System.out.println(this.getName()+"-"+this.getSex()+"-"+this.getAge());
        }
        
    }
    package com.yichang;
    
    public class Test2 {
    
        public static void main(String[] args) {
            Person p = new Person();
            try {
                p.setAge(150);
                p.print();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  • 相关阅读:
    svn搭建多版本共存记录
    python中使用redis
    小程序之使用腾讯地图获取经纬度
    vue路由元之进入路由需要用户登录权限功能
    input type="tel" 数字输入框显示圆点
    input在IOS中的聚焦问题
    JS实现手机号码中间4位变星号
    CSS实现div填充剩余高度
    小程序之地图导航
    小程序之点击图片放大预览
  • 原文地址:https://www.cnblogs.com/qq993411626------/p/10403901.html
Copyright © 2011-2022 走看看