
java基础_7_异常
Exception java.lang.NullPointerException ClassCastException Array indexOutofBoundsException java.lang.Object ____java.lang.throwable _java.lang.error //硬伤 _java.lang.Exception // Throwable: 可以抛出的 所有异常和错误的超类 图throw //抛出异常对象的指令 图throws //在方法中修饰抛出异常的关键字
异常细节
异常处理
throwable
发布日期:2021-05-15 00:26:31
浏览次数:23
分类:精选文章
本文共 2926 字,大约阅读时间需要 9 分钟。
异常





class Demo8{ public static void main(String[]args) { int []arr=null; //System.out.println(arr.length);空指针输出会出错 Exception e2=new Exception("出错了"); //打印异常消息 System.out.println(e2.getMessage()); //打印栈跟踪信息 } //异常处理,检查错误 public static void test (){ try{ throw new Exception("出错了"); }//需要检测的代码 catch(Exception e){ System.out.println(e.getMessage());//获取异常信息,返回字符串 }//异常处理代码 finally{ System.out.println("搞定了"); }//一定会输出的代码,有没有都可以 } }}
class Demo9{public static void main(String []args){int []arr={1,2,3,4,5};outArr(arr);}//输出数组public static void outArr(int [] arr){try{for(int i=0;i<
class Demo10{public static void main(String []args)throws AgeTooSmallException, AgeTooBigException//表示暴露异常{Person p=new Person();try{ p.setAge(201);}catch(Exception e){ System.out.println(e.getMessage());}System.out.println("over");}}class Person {private int age;//私有变量需要getset才能看到public int getAge(){return age; }public void setAge(int age)throws AgeTooSmallException, AgeTooBigException//表示暴露异常{if(age<0){ throw new AgeTooSmallException("年龄过小"); } if(age>200){ throw new AgeTooBigException("年龄过大"); } this.age=age; }}class AgeTooSmallException extends Exception{public AgeTooSmallException(){}public AgeTooSmallException(String mag){ super(mag); } } class AgeTooBigException extends Exception{public AgeTooBigException(){}public AgeTooBigException(String mag){ super(mag); } }
class Demo10{ public static void main(String[]args){ //outArr(arr); Person p =new Person(); try{ p.setAge(201); } catch(AgeTooSmallException e){ System.out.println("年龄带小了"); } catch(AgeTooBigException e){ System.out.println("年龄带大了"); }//细节的往前放 catch(Exception e){ System.out.println(e.getMessage()); } System.out.println("over"); }}class Person {private int age;public int getAge(){return age;}public void setAge(int age)throws AgeTooSmallException, AgeTooBigException{if(age<0){throw new AgeTooSmallException("年龄过小");}if(age>200){throw new AgeTooBigException("年龄过大");}this.age.age;}}class Student extends Person{public void setAge(int age)throws AgeTooSmallException{if(age<6){throw new InvalidAgeException("超出范围");}super.setAge(age);System.out.println("设置年龄over");}}class AgeTooSmallException extends Exception{ public AgeTooSmallException(){} public AgeTooSmallException(String mag){ super(mag); }}class AgeTooBigException extends RuntimeException//对比Runtime和非runtime(与Small对比{public AgeTooBigException (){}public AgeTooBigexception(String mag){super(mag);}}//自定义异常class InvalidAgeException extends AgeTooSmallException{ private String info; public InvalidAgeException (String info){ this.info=info; } public void outInfo(){ System.out.println(info); }}
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月24日 04时49分06秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Powershell中禁止执行脚本解决办法
2021-05-09
HTTP协议状态码详解(HTTP Status Code)
2021-05-09
OO_Unit2 多线程电梯总结
2021-05-09
04_Mysql配置文件(重要参数)
2021-05-09
JavaSE总结
2021-05-09
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
2021-05-09
Python IO编程
2021-05-09
CSS入门总结
2021-05-09
使用 TortoiseGit 时,报 Access denied 错误
2021-05-09
基于 HTML5 WebGL 的污水处理厂泵站自控系统
2021-05-09
django-表单之模型表单渲染(六)
2021-05-09
c++之程序流程控制
2021-05-09
spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
2021-05-09
有道云笔记 同步到我的博客园
2021-05-09
李笑来必读书籍整理
2021-05-09
Hadoop(十六)之使用Combiner优化MapReduce
2021-05-09
《机器学习Python实现_10_06_集成学习_boosting_gbdt分类实现》
2021-05-09