Java中异常处理

异常介绍

  1. 异常:编译正常通过,在运行过程中出现不正常情况
  2. 错误:单词或语法错误,程序无法正常编译
  3. bug:编译正常,运行正常,运行结果不正确
    出现异常可以使用if…else进行判断
public class Test {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入要统1-?之间的累加和");
        if(input.hasNextInt()){
            int number=input.nextInt();
            int sum=0;
            for(int i=1;i<=number;i++){
                sum+=i;
            }
            System.out.println(sum);
        }else {
            System.out.println("对不起,数据类型输入有误");
        }
    }
}

异常的分类

在这里插入图片描述

常见的异常类型

在这里插入图片描述
finally有唯一不执行情况,那就是使用System.exit(0)退出javaJVM

finally中一般放的是什么样的代码,IO流的关闭,数据库连接的关闭

异常的处理方式

  1. try -catch-finally [尝试解决]
    组合方式

    ①. try-catch
    产生了异常对象 ,产生的异常对象的类型与捕获的类型相同,或者是捕获类型的子类
    ②. try-finally
    ③.try-catch-finally

public class Test3 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入要统1-?之间的累加和");
        try {
            int number = input.nextInt();
            System.out.println("number="+number);
        }catch (Exception e){
            e.printStackTrace();//对象名.方法法,打印堆栈信息
        }finally {
            System.out.println("谢谢使用");
        }
        System.out.println("结束了");
    }
}

处理异常的代码统统放到catch块中
(1)自定义异常信息
(2)调用异常对象的方法,输出异常信息
toString ( )方法,显示异常的类名和产生异常的原因
void printStackTrace() 输出异常的堆栈信息
String getMessage()返回异常信息描述字符串,是printStackTrace()输出信息的一部分

public class Test3 {
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.println("请输入要统1-?之间的累加和");
        try {
            int number = input.nextInt();
            int result=number/0;
            System.out.println("number="+number);
        }catch (Exception e){
           //(1)自定义异常信息
           // System.err.println("程序出错了"); //System.err标准的错误流
            //(2)调用异常对象的方法
            System.err.println(e.toString()); //java.lang.ArithmeticException: / by zero 产生异常的类型,by zero产生异常的原因
            e.printStackTrace();
            System.err.println(e.getMessage()); //by zero
            //(3) 抛出异常对象
        
        }finally {
            System.out.println("谢谢使用");
        }
        System.out.println("结束了");
    }
}
  1. throws [可能会产生不正常的情况]
    throws在继承关系下使用时的注意事项

(1)父类没有声明异常,子类也不能声明Exception,如果子类的方法中真的有Exception,只能try-catch
(2)抛出的异常<=父类方法的异常的类型
3. throw [真的抛出了一个异常对象]

自定义异常类

自定义异常类的步骤
  1. 创建一个类继承Exception或RuntimeException
  2. 编写两个构造方法,一个无参构造 ,带异常详细描述的构造器
public class GenderException extends  Exception {
    public GenderException() {
    }
    public GenderException(String message) {
        super(message);
    }
}
public class Test2 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入您的性别:");
        String gender=input.next();
        if(!"男".equals(gender)&&!"女".equals(gender)){
            try {
                throw new GenderException("对不起,性别只能是男或者女");
            } catch (GenderException e) {
                e.printStackTrace();
            }
        }else{
            System.out.println("您输入的性别为:"+gender);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值