吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1429|回复: 7
收起左侧

[Java 转载] 五个最基本常见异常例子

[复制链接]
ny666999 发表于 2020-11-23 22:43
public class HomeWork01 {
        public static void main(String[] args) {
                System.out.println("数学异常.");
                try {
                        int a = 1 / 0;
                        System.out.println(a);
                } catch (ArithmeticException exception) {
                        System.out.println("异常了");
                } finally {
                        System.out.println("数学异常结束.");
                }
                /***************************************************************/
                System.out.println("空指针异常");
                try {
                        String string = new String();
                        string = null;
                        System.out.println(string.length());
                } catch (NullPointerException exception) {
                        System.out.println("异常了");
                } finally {
                        System.out.println("空指针异常结束.");
                }
                /***************************************************************/
                System.out.println("数学格式异常");
                try {
                        String str = "aaa123";
                        Integer integer = new Integer(str);
                        System.out.println(integer);

                } catch (NumberFormatException exception) {
                        System.out.println("异常了");
                } finally {
                        System.out.println("数学格式异常结束");
                }
                /***************************************************************/
                System.out.println("索引越界异常");
                try {
                        int a[] = { 0, 1 };
                        System.out.println(a[2]);
                } catch (ArrayIndexOutOfBoundsException exception) {
                        System.out.println("异常了");
                } finally {
                        System.out.println("索引越界异常结束");
                }
                /***************************************************************/
                System.out.println("类型转换异常");
                try {
                        A a = new C();
                        B b = (B) a;
                        System.out.println(b);
                } catch (ClassCastException exception) {
                        System.out.println("异常了");
                } finally {
                        System.out.println("类型转换异常结束");
                }
        }
}

class A {
}

class B extends A {
}

class C extends A {
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

angel_bai 发表于 2020-11-24 12:47
学习学习
netspirit 发表于 2020-11-24 05:45
xfwb 发表于 2020-11-24 07:13
荒天 发表于 2020-11-24 08:12
System.out.println("楼主的帖子非常异常")
晨露有点儿甜 发表于 2020-11-24 08:15
谢谢分享!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-15 16:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表