栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java——异常Exception

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java——异常Exception

目录
  • 1.异常介绍
  • 2.常见的运行时异常
    • 1.空指针异常
    • 2.数学运算异常
    • 3.数组下标越界异常
    • 4.类型转换异常
    • 5.数字格式不正确异常

1.异常介绍

基本概念

程序执行中发生的不正常情况称为异常。(语法错误和逻辑错误不是异常)。

package com.demo;


public class exception01 {
    public static void main(String[] args)  {
        int num1 = 10;
        int num2 = 0;//Scanner();

        //1. num1 / num2 => 10 / 0
        //2. 当执行到 num1 / num2 因为 num2 = 0, 程序就会出现(抛出)异常 ArithmeticException
        //3. 当抛出异常后,程序就退出,崩溃了 , 下面的代码就不在执行
        //4. 不应该出现了一个不算致命的问题,就导致整个系统崩溃
        //5. 我们可以使用异常处理机制来解决该问题
//        int res = num1 / num2;
        //如果程序员,认为一段代码可能出现异常/问题,可以使用try-catch异常处理机制来解决
        //从而保证程序的健壮性
        //将该代码块->选中->快捷键 ctrl + alt + t -> 选中 try-catch
        //6. 如果进行异常处理,那么即使出现了异常,程序可以继续执行
        try {
            int res = num1 / num2;
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("出现异常的原因=" + e.getMessage());//输出异常信息
        }

        System.out.println("程序继续运行....");

    }
}

2.常见的运行时异常 1.空指针异常
package com.demo;


public class NullPointerExceotion {
        public static void main(String[] args) {

            String name01 = null;
            try {
                System.out.println(name01.length());
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
            String name02="sdasd";
            System.out.println(name02.length());
        }
}

2.数学运算异常
package com.demo;


public class exception01 {
    public static void main(String[] args)  {
        int num1 = 10;
        int num2 = 0;//Scanner();

        //1. num1 / num2 => 10 / 0
        //2. 当执行到 num1 / num2 因为 num2 = 0, 程序就会出现(抛出)异常 ArithmeticException
        //3. 当抛出异常后,程序就退出,崩溃了 , 下面的代码就不在执行
        //4. 大家想想这样的程序好吗? 不好,不应该出现了一个不算致命的问题,就导致整个系统崩溃
        //5. java 设计者,提供了一个叫 异常处理机制来解决该问题
//        int res = num1 / num2;
        //如果程序员,认为一段代码可能出现异常/问题,可以使用try-catch异常处理机制来解决
        //从而保证程序的健壮性
        //将该代码块->选中->快捷键 ctrl + alt + t -> 选中 try-catch
        //6. 如果进行异常处理,那么即使出现了异常,程序可以继续执行
        try {
            int res = num1 / num2;
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("出现异常的原因=" + e.getMessage());//输出异常信息
        }

        System.out.println("程序继续运行....");

    }
}
3.数组下标越界异常
package com.demo;


public class ArrayIndexOutOfBoundException_ {
    public static void main(String[] args) {
        int []arr={5,6,8};
//        for (int i = 0; i < arr.length; i++) {
//            System.out.println(arr[i]);
//        }
        for (int i = 0; i <= arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

4.类型转换异常
package com.demo;


public class ClassCastException_ {
    public static void main(String[] args) {
        A b = new B(); //向上转型
        B b2 = (B)b;//向下转型,这里是OK
        C c2 = (C)b;//这里抛出ClassCastException
    }
}
class A {}
class B extends A {}
class C extends A {}

5.数字格式不正确异常
package com.demo;


public class NumberFormatException_ {
    public static void main(String[] args) {
        String ch01="265450";
        //将String转为int
        int num01=Integer.parseInt(ch01);
        System.out.println("num01="+num01);
        String ch02="Demo龙";
        //将String转为int
        int num02=Integer.parseInt(ch02);
        System.out.println("num02="+num02);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932552.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号