做外贸一般看什么网站,东莞快速优化排名,中国做的好的房产网站,韶关住房和城乡建设部网站Exception中有一个特殊的子类异常RuntimeException运行时异常。如果在函数内抛出该异常#xff0c;函数上可以不用声明#xff0c;编译一样通过。如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过。之所以不用在函数上声明#xff0c;是因为不需要让调用者处…Exception中有一个特殊的子类异常RuntimeException运行时异常。如果在函数内抛出该异常函数上可以不用声明编译一样通过。如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过。之所以不用在函数上声明是因为不需要让调用者处理。当该异常发生希望程序停止因为在运行时出现了无法继续运算的情况希望停止程序后对代码进行修正。自定义异常时如果该异常的发生无法再继续进行运算就让自定义异常继承RuntimeException.对于异常分两种1、编译时被监测的异常2、编译时不被监测的异常(运行时异常RuntimeException以及其子类)class FuShuException extends RuntimeException {FuShuException(String msg){super(msg);}}class Demo {int div(int a,int b)//函数上没有抛出异常因为FuShuException是RuntimeException的子类{if(b0)throw new FuShuException(除数为负数);if(b0)throw new ArithmeticException(被0除了);return a/b;}}class ExceptionDemo7 {public static void main(String[] args){Demo d new Demo();int x d.div(4,-4);System.out.println(xx);}}输出结果Exception in thread main FuShuException: 除数为负数at Demo.div(ExceptionDemo00.java:15)at ExceptionDemo00.main(ExceptionDemo00.java:28)举例问题是电脑冒烟电脑蓝屏要对问题进行描述封装成对象。可是当冒烟发生后出现讲课进度无法继续。出现了老师的问题课时计划无法完成class LanPingException extends Exception {LanPingException(String message){super(message);}}class MaoYanException extends Exception {MaoYanException(String message){super(message);}}class NoPlanException extends Exception {NoPlanException(String msg){super(msg);}}class Computer {private int state 3;public void run()throws LanPingException,MaoYanException{if(state2)throw new LanPingException(蓝屏了);if(state3)throw new MaoYanException(冒烟了);System.out.println(电脑运行);}public void reset(){state 1;System.out.println(电脑重启);}}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(LanPingException e){cmpt.reset();}catch(MaoYanException e){test();throw new NoPlanException(课时无法继续e.getMessage());}System.out.println(讲课了);}public void test(){System.out.println(LianXi);}}class ExceptionTest {public static void main(String[] args){Teacher t new Teacher(Wang);try{t.prelect();}catch(NoPlanException e){System.out.println(e.toString());System.out.println(换老师或者放假);}}}运行结果LianXiNoPlanException: 课时无法继续冒烟了换老师或者放假举例有一个圆形和长方形。都可以获取面积。对于面积如果出现非法的数值视为是获取面积出现问题。问题通过异常来表示。现在对这个程序进行基本设计。//定义一个异常让它继承运行时异常class NoValueException extends RuntimeException {NoValueException(String msg){super(msg);}}//定义一个接口interface Shape {void getArea();}//长方形实现Shape接口class Rectangle implements Shape {private int len,wid;Rectangle(int len,int wid)//这里不用throws NoValueException因为NoValueException是RuntimeException的子类{if(len0||wid0)throw new NoValueException(出现非法值);this.len len;this.wid wid;}public void getArea(){System.out.println(len*wid);}}class Circle implements Shape {private double radius;public static final double PI 3.14;Circle(double radius){if(radius0)throw new NoValueException(非法);this.radius radius;}public void getArea(){System.out.println(PI*radius*radius);}}class Demo2{public static void main(String[] args){Rectangle r new Rectangle(3,4);r.getArea();Circle circle new Circle(-8);circle.getArea();System.out.println(over);}}