JAVA中对异常抛出时的疑问

怎么才能使输入字母时,把错误抛出来呢?怎么样才能在输入错误数字后,重新在输入呢?求解答,各位大佬们,来自一位初学者请求!嘿嘿嘿
在这里插入图片描述
在这里插入图片描述

异常类
package com.company;

public class OutException extends Exception {
public OutException(){
super();
}
public OutException(String message){
super(message);
}
}
方法类
package com.company;
import java.util.InputMismatchException;
import java.util.Scanner;
public class EmployeeTest {

public static void main(String[] args) {
        int employeeId;
        int department;
        String employeeName;
    Scanner sc = new Scanner(System.in);
    System.out.println("----欢迎进入测试异常员工查询系统----");
    System.out.println("您已进入程序,请进行测试!");
    System.out.println("请输入您要选择的部门");
    department = sc.nextInt();
    try {
        switch (department) {
            case 1:
                System.out.println("欢迎进入1部门");
                break;
            case 2:
                System.out.println("欢迎进入2部门");
                break;
            case 3:
                System.out.println("欢迎进入3部门");
                break;
            case 4:
                System.out.println("欢迎进入4部门");
                break;
            case 5:
                System.out.println("欢迎进入5部门");
                break;
            default:
                System.out.println();
                if (department > 6) {
                    System.out.println("对不起,并未查询到所选编号部门");
                    throw new OutException("您所选择的部门大于了数字5");
                }else if (department!=sc.nextInt()){
                    throw new InputMismatchException(){};
                }
        }
    } catch (InputMismatchException I) {
        System.out.println(("对不起您输入的数字类型错误"));
    } catch (OutException O) {
        System.out.println(O.getMessage());

    } finally {
        System.out.println("请输入您要选择的员工编号");
        employeeId = sc.nextInt();
        System.out.println("请输入您要查询的员工姓名");
        employeeName = sc.next();
        System.out.println("员工编号:" + employeeId +"  "+ "姓名:" + employeeName+"  " +
                "部门编号:" + department);
    }

}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值