JAVA学习日志(9-3-异常练习)
2016-08-02 00:00
183 查看
Exception中有一个特殊的子类异常 RuntimeException 运行时异常
如果在函数内容中抛出了该异常,函数上可以不用声明,编译一样通过
如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过
之所以不用在函数上声明,是因为不需要让调用者处理
当该异常发生,希望程序停止
因为在运行时,出现了无法继续运算的情况,希望停止程序后
对代码进行修正
自定义异常时,如果该异常的发生,无法再继续进行运算
就让自定义异常继承RuntimeException
对于异常分为两种:
1.编译时被检测的异常
2.编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
异常练习
v1.0
v2.0 加入异常
如果在函数内容中抛出了该异常,函数上可以不用声明,编译一样通过
如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过
之所以不用在函数上声明,是因为不需要让调用者处理
当该异常发生,希望程序停止
因为在运行时,出现了无法继续运算的情况,希望停止程序后
对代码进行修正
class rDemo{ int div(int a,int b){ if(b==0){ throw new ArithmeticException("除数为零"); } return a/b; } } class ExceptionDemo3{ public static void main(String[] args){ rDemo r=new rDemo(); int x=r.div(4,0); System.out.println("x="+x); System.out.println("over"); } } /*编译通过,输出结果为: Exception in thread "main" java.lang.ArithmeticException: 除数为零 at rDemo.div(ExceptionDemo3.java:4) at ExceptionDemo3.main(ExceptionDemo3.java:12) */
自定义异常时,如果该异常的发生,无法再继续进行运算
就让自定义异常继承RuntimeException
class FuShuException extends RuntimeException{ FuShuException(String msg){ super(msg); } } class Demo{ int div(int a,int b){ if(b<0){ throw new FuShuException("除数为负数"); } if(b==0){ throw new ArithmeticException("除数为零"); } return a/b; } } class ExceptionDemo4{ public static void main(String[] args){ Demo d=new Demo(); int x=d.div(4,-1); System.out.println("x="+x); System.out.println("OVER"); } }
对于异常分为两种:
1.编译时被检测的异常
2.编译时不被检测的异常(运行时异常,RuntimeException以及其子类)
异常练习
v1.0
/* 需求:老师用电脑上课 */ class Computer{ public void run(){ System.out.println("Computer_Run"); } public void reset(){ System.out.println("Computer_Reset"); } } class Teacher{ private String name; private Computer cmpt; Teacher(String name){ this.name=name; cmpt=new Computer(); } public void prelect(){ cmpt.run(); System.out.println("Teacher_Run"); } } class ExceptionTest{ public static void main(String[] args){ Teacher t=new Teacher("毕老师"); t.prelect(); } }
v2.0 加入异常
/* 需求:老师用电脑上课 上课中的异常:电脑蓝屏 电脑冒烟 */ class bluesrException extends Exception{ //将异常进行描述,封装成对象 bluesrException(String message){ super(message); } } class smokeException extends Exception{ //冒烟发生后讲课进度无法继续 smokeException(String message){ //出现讲师异常 super(message); } } class noPlanException extends Exception{ noPlanException(String message){ super(message); } } class Computer{ private int state=3; public void run()throws bluesrException,smokeException{ if(state==2){ throw new bluesrException("蓝屏"); } if(state==3){ throw new smokeException("冒烟"); } System.out.println("Computer_Run"); } public void reset(){ state=1; System.out.println("Computer_Reset"); } } class Teacher{ private String name; private Computer cmpt; Teacher(String name){ this.name=name; cmpt=new Computer(); } public void prelect()throws noPlanException{ try{ cmpt.run(); } catch(bluesrException e){ cmpt.reset(); } catch(smokeException e){ //抛出讲师异常 test(); throw new noPlanException("课时无法继续"+e.getMessage()); //和return一样属于结束语句 } System.out.println("Teacher_Run"); } public void test(){ System.out.println("Test"); } } class ExceptionTest{ public static void main(String[] args){ Teacher t=new Teacher("毕老师"); try{ t.prelect(); } catch(noPlanException e){ System.out.println(e.toString()) 3ff0 ; System.out.println("换老师或者放假"); } } }
相关文章推荐
- 【JAVA学习】11.X JAVA异常处理机制练习
- [Java学习日志]图形界面开发练习——绘图板的实现
- JAVA练习-第3天 有关异常处理的学习
- java基础学习日志--异常案例
- [core java学习笔记][第十一章异常断言日志调试]
- Java学习笔记——异常、断言和日志
- JAVA学习日志(10-1-异常覆盖特点及总结)
- 黑马程序员 java中关于异常的学习日志
- [core java学习笔记][第十一章异常断言日志调试]
- 阿里Java开发手册学习 2 异常日志
- [core java学习笔记][第十一章异常断言日志调试]
- java学习日志——异常
- JAVA学习日志(9-2-异常)
- Java学习之异常练习
- 黑马程序员——java基础:异常学习日志
- 学习笔记 ---- Core Java: 十一章异常、日志、断言和调试
- 阿里巴巴Java开发手册学习小结8-异常日志
- java异常学习
- JAVA异常学习笔记
- java学习日志----望老鸟指教啊^_^