异常

来源:互联网 发布:厘米换算英尺英寸c语言 编辑:程序博客网 时间:2024/06/05 23:49

异常

class ExceptionDemo {public static void main(String[] args) {Demo d = new Demo();try{int x = d.div(4,0);System.out.println(x);}catch (Exception e){System.out.println("zzz");System.out.println(e.getMessage());//异常信息System.out.println(e.toString());e.printStackTrace();//异常名称 异常信息 异常出现的位置}}}class Demo{int div(int a, int b)throws Exception {return a/b;}}


多异常


class ExceptionDemo1 {public static void main(String[] args) {Demo d = new Demo();try{int x = d.div(3,1);System.out.println(x);}catch (ArithmeticException e){System.out.println(e.toString());System.out.println("被零除");}catch(ArrayIndexOutOfBoundsException e){System.out.println(e.toString());System.out.println("角标越界");}}}class Demo{int div(int a, int b)throws ArithmeticException,ArrayIndexOutOfBoundsException {int [] arr = new int[a];System.out.println(arr[4]);return a/b;}}

自定义异常

class Demo{int div(int a, int b)throws FuException {if(b<0)throw new FuException("zzzzz",b);return a/b;}}class FuException extends Exception{private int value;FuException(){}FuException(String msg,int value){super(msg);this.value = value;}public int getValue(){return value;}}class ExceptionDemo2 {public static void main(String[] args) {Demo d = new Demo();try{int x = d.div(4,-1);System.out.println(x);}catch (FuException e){System.out.println(e.toString());System.out.println("错误是:"+e.getValue());}}}


阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 声音的英文 声音好听 声音英语 什么的声音 说话声音小 手机声音小 耳机声音小 音响声音小 声音大手机 火警声音mp3 心跳的声音 声音有 什么声音 各种声音 听声音 有声音 声音的特质 表示声音的 表示声音 雪落下的声音 手机听筒声音小 手机听筒声音小怎么办 表示声音词语 詹雯婷声音辨识度 梦想声音第三季 形容声音成语 电脑没有声音怎么办 形容声音词语 aabb表示声音 用嗓过度声音嘶哑 唱吧声音小怎么办 iphone听筒声音小怎么办 手机话筒声音小怎么调 唱歌声音小怎么办 手机声音小是怎么回事 声音是纵波还是横波 手机麦克风声音小 怎样使声音更好听 耳朵听不见声音怎么回事 电脑主机声音好大 耳鸣听不到声音怎么办