黑马程序员---Java基础(异常机制)
2015-10-05 09:09
357 查看
-----Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
以下是有关异常的知识点总结:
this:代表本类对象,哪个对象调用this所在的函数,this就代表哪个对象
final:
1.修饰类。变量(成员变量,静态变量,局部变量)。函数。
2.修饰的类不可以被继承
3.修饰的函数不可以被覆盖
4.修饰的变量是一个常量,只能赋值一次。
-----Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
以下是有关异常的知识点总结:
/*
异常:就是程序在运行时出现不正常的情况。 异常的由来:问题也是现实生活中一个具体的事物, 也可以通过java的类的形成进行描述,并封装 成对象。 对于问题的划分:两种:一种是严重的问题 一种是非严重的问题 对于严重的问题:java通过Error类进行描述 对于Error一般不填写针对性的代码进行处理 对于非严重的,java通过Exception类进行描述 对于Exception可以使用针对性的处理方式进行处理 无论Error或者Exception,都具有一些共性的内容 比如:不正常情况的信息,引发的原因等。 Throwable |----Error |----Exception 异常的处理: java提供了特有的语句进行处理 try { 需要被检测的代码; } catch(异常类 变量) { 处理异常的代码:(处理方式) } finally { 一定会执行的语句; } 对捕获的异常对象进行常见方法操作。 String getMessage():获取异常信息。 在函数上声明异常。 便于提高安全性,让调用者进行处理,不处理编译失败 对多异常的处理。 1.声明异常时,建议声明更为具体的异常。这样处理的可以更具体 2.对方声明几个异常,就对应有几个catch块,不要定义多余的catch块。 如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。 建议在进行catch处理时,catch中一定要定义具体的处理方式 不要简单定义一句e.printStackTrace(); 也不要简单的就书写一条输出语句 */ class Demo { int div(int a,int b) { return a/b; } } class ExceptionDemo { public static void main(String[] args) { Demo d= new Demo(); try { int x = d.div(4,0); System.out.println("x="+x); } catch (Exception e) //Exception e = new ArithmeticException(); { System.out.println("除零啦"); System.out.println(e.getMessage());//by zero System.out.println(e.toString());//异常名称,异常信息 e.printStackTrace();//异常名称,异常信息,异常出现干的位置 //其实jvm默认的异常处理机制,就是在调用printStackTrace方法。 //打印异常的堆栈的跟踪信息 } System.out.println("over"); } }
/* 自定义异常: 需求:在本程序中,对于除数是-1,也视为是错误,是无法处理的 那么就需要对这个问题进行自定义的描述 当在函数中内部出现了throw抛出异常对象。那么就必须要给对应的处理动作 要么在内部try catch处理 要么在函数上声明让调用者处理 一般情况在,函数内出现异常,函数上需要声明 发现打印的结果中只有异常的名称,却没有异常的信息 因为自定义的异常并未定义信息 如何定义异常信息呢? 因为父类中已经把异常的操作都完成了, 所以子类只要在构造时,将异常信息传递给父类通过super语句 那么就可以直接通过getMessage方法获取自定义的异常信息。 自定义异常: 必须是自定义类继承Exception 继承Exception的原因: 异常体系有一个特点:因为异常类和异常对象都被抛出。 他们都具备可抛性,这个可抛性是Throwable这个体系中独有特点。 只有这个体系中的类和对象才可以被throw和throws操作. throws和throw的区别 throws使用在函数上, throw使用在函数内 throws后面跟异常类,可以跟多个,用逗号隔开 throw后跟的是异常对象 */ class FuShuException extends Exception { private int value; FuShuExcepion() { super(); } FuShuExcepion(String msg,int value) { super(msg); this.value = value; } public int getValue() { return value; } } class Demo { int div(int a,int b)throws FuShuException { //手动通过throw关键字抛出一个自定义异常 if(b<0) throw new FuShuException("出现了除数是负数的情况--/by fushu"); return a/b; } } class ExceptionDemo3 { public static void main(String[] args) { Demo d = new Demo(); try { int x = d.div(4,-1); System.out.println("x="+x); } catch (FuShuException e) { System.out.println(e.toString()); System.out.println("除数出现负数了"); System.out.println("错误的负数是:"+e.getValue()); } System.out.println("over"); } } /* class Throwable { private String message; Throwable(String message) { this.message = message; } public String getMessage() { return message; } } */
/* Exception中有一个特殊的子类异常RuntimeException运行时异常 如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。 如果在函数上声明了该异常,调用者可以不用进行处理,编译一样通过。 之所以不用在函数声明,是因为不需要让调用者处理 当该异常发生。希望程序停止。因为在运行时,出现了无法继续 运行的情况,希望停止程序后,对代码进行修正。 自定义异常时,如果该异常的发生,无法在继续进行运算。 就让自定义异常继承RuntimeException 对于异常分两种: 1.编译时被检测到的异常 2.编译时不被检测的异常(运行时异常,RuntimeException及其子类) 注意:在自定义异常时,如何问题发生后,可以被处理,处理之后程序 可以继续运行,那就继承Exception。如果问题不能被处理,需要修正 代码的时候,那就继承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 ExceptionDemo5 { public static void main(String[] args) { Demo d = new Demo(); int x = d.div(4,-9); System.out.println("x="+x); System.out.println("over"); } }
/* class Person { <span style="white-space:pre"> </span>public void checkName(String name) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//if(name.equals("lisi")) //NullPointerException <span style="white-space:pre"> </span>if("lisi".equals(name)) //等于if(name!=null&&name.equals("lisi")) <span style="white-space:pre"> </span> System.out.println("YES"); <span style="white-space:pre"> </span> else <span style="white-space:pre"> </span> System.out.println("NO"); <span style="white-space:pre"> </span>} } main() { <span style="white-space:pre"> </span>Person p = new Person(); <span style="white-space:pre"> </span>p.checkName(null); } */下面请看举例:(仔细体会异常的应用)
/* 毕老师用电脑上课。 开始思考上课中出现的问题。 比如问题是: 电脑蓝屏 电脑冒烟 要对问题进行描述,封装成对象 可是当冒烟发生后,出现讲课进度无法继续。 出现了讲师的问题,课时的计划完不成了 */ class LanPingException extends Exception { LanPingException(String message) { super(message); } } class MaoYanException extends Exception { MaoYanException(String message) { super(message); } } class NoPlanException extends Exception { NoPlanException(Strin 4000 g message) { super(message); } } class Computer { private int state = 2; public void run() throws LanPingException,MaoYanException { if(state==2) throw new LanPingException("蓝屏了!!"); if(state==3) throw new MaoYanException("冒烟了!!"); System.out.println("电脑运行"); } public void reset() { state = 1; System.out.println("电脑重启"); } } class Teacher { private String name; private Computer cmp; Teacher(String name) { this.name = name; cmp = new Computer(); } public void preselect() throws NoPlanException { try { cmp.run(); } catch (LanPingException e) { cmp.reset(); } catch (MaoYanException e) { test(); throw new NoPlanException("课时无法继续!!"+e.getMessage()); //test(); //方法不能写在throw 之后,编译时显示无法访问到此语句 //因为抛出之后,程序就结束了,后面的代码就不会运行了 //记住:return、throw 之后,不要写语句了 } public void test() { System.out.println("做练习"); } System.out.println("老师讲课了!!!"); } } class ExceptionTest { public static void main(String[] args) { Teacher t = new Teacher("bilaoshi"); try { t.preselect(); } catch (NoPlanException e) { System.out.println(e.toString()); System.out.println("换老师或者放假!!"); } } }
/* finally代码块:定义一定执行的代码。 通常用于关闭资源 */ public void method() { try { 连接数据库; 数据操作;//throw new SQLException(); } catch (SQLException e) { 会对数据库进行异常处理; } finally { 关闭数据库 } }
/* 第一个格式: try { } catch() { } 第二个格式: try { } catch() { } finally { } 第三个格式: try { } finally { } 记住一点:catch是用于处理异常,如果没有catch就代表 异常没有被处理过,如果该异常是检测时异常,那么必须声明 异常在子父类覆盖中的体现: 1.子类在覆盖父类时,如果父类的方法抛出异常, 那么子类的覆盖方法,只能抛出父类的异常或者 该异常的子类 2.如果父类方法抛出多个异常,那么子类在覆盖 方法时,只能抛出父类异常的子集 3.如果父类或者接口的方法中没有异常抛出,那么 子类在覆盖方法时,也不可以抛出异常。 如果子类方法发生了异常,就必须要进行try处理, 绝不能抛出。 */
/* 异常总结: 异常是什么?是对问题的描述,将问题进行对象的封装。 异常体系: Throwable ---Error ---Exception ----RuntimeException 异常体系的特点: 异常体系中所有的类以及建立的对象都具备可抛性。 也就是说可以被throw和throws关键字所操作。 只有异常体系具备这个特点。 throws和throw的用法: throw定义在函数内,用于抛出异常对象。 throws定义在函数上,用于抛出异常类,可以抛出多个, 用逗号隔开 当函数内容有throw抛出异常对象,并未进行try处理,必须要在 函数上声明,否则编译失败 注意:RuntimeException除外,函数内如果抛出RuntimeException异常, 函数上可以不用声明。 如果函数声明了异常,调用者需要进行处理,处理方法可以throws或者try 异常有两种: 编译时被检测异常 该异常在编译时,如果没有处理(没有抛出也没有try),编译失败 该异常被标识,代表这可以被处理。 运行时异常(编译时不检测) 在编译时,不需要处理,编译器不检查 该异常的发生,建议不处理,让程序停止, 需要对代码进行修正。 异常处理语句: try { 需要被检测的代码; } catch () { 处理异常的代码; } finally { 一定会执行的代码; } 有三个结合格式: try { } catch () { } try { } finally { } try { } catch () { } finally { } 注意:1.finally中定义的通常是关闭资源代码, 因为资源必须释放 2.finally只有一种情况不会执行,当执行到 System.exit(0); 自定义异常:定义类继承Exception或者RuntimeException 1.为了让该自定义类具备可抛性。 2.让该类具备操作异常的共性方法。 当要定义自定义异常的信息时,可以使用父类已经的定义 好的功能。 Class MyException extends Exception { MyException(String message) { super(message); } } 自定义异常:按照java的面向对象思想,将程序中出现的 特有问题进行封装。 异常的好处: 1.将问题进行封装 2.将正常流程代码和问题处理代码相分离 方便与阅读。 异常的处理原则: 1.处理方式有两种:try或者throws 2.调用到抛出异常的功能时,抛出几个,就处理几个。 一个try对应多个catch。 3.多个catch,父类的catch放在最下面。 4.catch内,需要定义针对性的处理方式。 不要简单定义printStackTrace,输出语句。 也不要不写。 try { throw new AException(); } catch (AException e) { throw e; } 如果该异常处理不了,但并不属于该功能出现的异常。 可以将异常转换后,在抛出和该功能相关的异常。 或者异常可以处理,当需要将异常产生的和本功能 相关的问题提供出去。 当调用这知道,并处理。也可以将捕获异常处理后, 转换新的异常。 try { throw new AException(); } catch (AException e) { //对AException处理 throw new BException } 异常的注意事项: 在子父类覆盖时: 1.子类抛出的异常必须是父类的异常的子类 或者子集 2.如果父类或者接口没有异常抛出时, 子类覆盖出现异常,只能try不能抛。 */以下是练习题:
/* 第一题*/ class Demo { public static void func() //throws Exception { try { throw new Exception(); } finally { System.out.println("B"); } } public static void main(String[] args) { try { func(); System.out.println("A"); } catch (Exception e) { System.out.println("C"); } System.out.println("D"); } } /* 编译失败:func函数,只是try,但是没有catch,也就是没有处理,必须在函数上声明 所以必须在func函数上声明该异常,如果声明了该异常的话,结果是:B C D */
/*第二题:写出程序结果*/ class Test { Test() { System.out.println("Test"); } } class Demo2 extends Test { Demo2() { //super(); System.out.println("Demo"); } public static void main(String[] args) { new Demo2(); new Test(); } } /* 结果: Test Demo Test 考的是子类的实例化过程 */
/*第3题:写出程序结果*/ interface A { } class B implements A { public String func() { return "func"; } } class Demo3 { public static void main(String[] args) { A a = new B(); System.out.println(a.func()); } } /* 编译失败:因为A接口中并未定义func方法。 */
/*第4题:写出程序结果*/ class Fu { boolean show(char a) { System.out.println(a); return true; } } class Demo4 extends Fu { public static void main(String[] args) { int i = 0; Fu f = new Demo(); Demo d = new Demo(); for(f.show('A');f.show('B')&&(i<2);f.show('C')) { i++; d.show('D'); } boolean show(char a) { System.out.println(a); return false; } } } /* 多态中,编译看父类,运行看子类 结果是: A B */
/*第5题:写出程序结果*/ interface A { } class B implements A { public String test() { return "yes"; } } class Demo5 { static A get() { return new B(); } public static void main(String[] args) { A a = get(); System.out.println(a.test()); } } /* 编译失败;因为A接口中没有定义test()方法; */
/*第6题:写出程序结果*/ class Super { int i = 0; public Super(String a) { System.out.println("A"); i = 1; } public Super() { System.out.println("B"); i += 2; } } class Demo6 extends Super { public Demo6(String a) { //super(); System.out.println("C"); i = 5; } public static void main(String[] args) { int i = 4; Super d = new Demo6("A"); System.out.println(d.i); } } /* 结果是: B C 5 */
/*第7题:答案为补足代码部分*/ interface Inter { void show(int a,int b); void func(); } class Demo7 { public static void main(String[] args) { //补足代码,调用两个函数,要求用匿名内部类 Inter d = new Inter() { public void show(int a,int b) { System.out.println("show方法"); } public void func() { System.out.println("func方法"); } }; d.show(1,2); d.func(); } }
/*第8题:写出程序结果*/ class TD { int y = 6; class Inner { static int y = 3; void show() { System.out.priintln(y); } } } class Demo8 { public static void main(String[] args) { TD.Inner ti = new TD().new Inner(); ti.show(); } } /* 编译失败:非静态内部类中不可以定义静态成员; 内部类中如果定义了静态成员,该内部类必须被静态修饰。 */
/* 9.选择题,写出错误答案错误的原因,用单行注释的方式。 class Demo { int show(int a,int b){return 0;}; } 下面那些函数可以存在于Demo的子类中。 A.public int show(int a,int b){return 0;} //可以,覆盖。 B.private int show(int a,int b){return 0;} //不可以,权限不够 C.private int show(int a,long b){return 0;} //可以,和父类不是一个函数,没有覆盖,相当于重载 D.public short show(int a,int b){return 0;} //不可以,该函数不可以和给定函数出现在同一个类中,或者子父类中(无法覆盖 父类中的方法) E.static int show(int a,int b){return 0;} //不可以,静态只能覆盖静态
*/10:this,final关键字
this:代表本类对象,哪个对象调用this所在的函数,this就代表哪个对象
final:
1.修饰类。变量(成员变量,静态变量,局部变量)。函数。
2.修饰的类不可以被继承
3.修饰的函数不可以被覆盖
4.修饰的变量是一个常量,只能赋值一次。
/*第11题,写出程序结果:*/ class Fu { int num = 4; void show() { System.out.println("showFu"); } } class Zi extends Fu { int num = 5; void show() { System.out.println("showZi"); } } class Demo11 { public static void main(String[] args) { Fu f = new Zi(); Zi z = new Zi(); System.out.println(f.num); System.out.println(z.num); f.show(); z.show(); } } /* 变量不存在覆盖,成员变量看左边 结果是: 4 5 showzi showzi */
/* 第12题:补写程序代码*/ interface A { void show(); } interface B { void add(int a,int b); } class C implements A,B { //程序代码 private int sum; public void add(int a,int b) { sum = a+b; } public void show() { System.out.println(sum); } } class Demo12 { public static void main(String[] args) { C c = new C(); c.add(4,2); c.show();//通过该函数打印以上两个数的和。 } }
/*13.写出程序结果*/ class Demo13 { public static void main(String[] args) { try { showExce(); System.out.println("A"); } catch (Exception e) { System.out.println("B"); } finally { System.out.println("C"); } System.out.println("D"); } public static void showExce() throws Exception { throw new Exception(); } } /* 结果是: B C D */
/*14.写出程序结果*/ class Super { int i = 0; public Super(String s) { i = 1; } } class Demo14 extends Super { public Demo(String s) { i = 2; } public static void main(String[] args) { Demo d = new Demo("yes"); System.out.println(d.i); } } /* 编译失败,因为父类中缺少空参数的构造函数。 或者子类应该通过super语句指定要调用的父类中的构造函数。 */
/*15.写出程序结果*/ class Super { public int get(){return 4;} } class Demo15 extends Super { public long get(){return 5;} public static void main(String[] args) { Super s = new Demo15(); System.out.println(s.get()); } } /* 编译失败,因为子类父类中的get方法没有覆盖,但是子类调用时候不能明确返回的值是什么类型。 所以这样的函数不可以存在于父类中。。 */
/*16. 写出程序结果*/ class Demo16 { public static void func() { try { throw new Exception(); System.out.println("A"); } catch (Exception e) { System.out.println("B"); } } public static void main(String[] args) { try { func(); } catch (Exception e) { System.out.println("C"); } System.out.println("D"); } } /* 结果是: 错误: 无法访问的语句 System.out.println("A"); ^ 编译失败:因为打印‘A’的输出语句执行不到 和13题区分开: 记住:throw单独存在,下面不要定义语句,因为执行不到!! */
/* 第17题: class Demo { public void func() { //位置1; } class Inner{} public static void main(String[] args) { Demo d = new Demo(); //位置2 } } A.在位置1写 new Inner(); //ok B.在位置2写 new Inner(); //不可以,因为主函数是静态的,如果要访问inner需要被static修饰 C.在位置2写 new d.Inner(); //错误,格式错误 D.在位置2写 new Demo.Inner();//错误,因为inner不是静态的。 */
/*18.写出程序结果*/ class Exc0 extends Exception { } class Exc1 extends Exc0 { } class Demo18 { public static void main(String[] args) { try { throw new Exc1(); } catch (Exception e) { System.out.println("Exception"); } catch(Exc0 e) { System.out.println("Exc0"); } } } /* 错误: 已捕获到异常错误Exc0 catch(Exc0 e) ^ 编译失败:多个catch时,父类的catch要放在下面 */
/* 19.(看补足代码部分) interface Test { void func(); } class Demo { public static void main(String[] args) { //补足代码;(匿名内部类) new Demo().show(new Test() { public void func(){} } ); } void show(Test t) { t.func(); } } */
/*20.写出程序结果*/ class Demo20 { public static String output = ""; public static void foo(int i) { try { if(i==1) throw new Exception(); output +="1"; } catch (Exception e) { output += "2"; return ; //因为有return,所以output += "4";这句就不执行了 } finally { output += "3"; } output += "4"; } public static void main(String[] args) { foo(0); System.out.println(output); foo(1); System.out.println(output); } } /* 结果是: 134 13423 */
/*
21.补足代码,不允许添加其他函数
*/ class Circle { private static double pi = 3.14; private double radius; public Circle(double r) { radius = r; } public static double compare(Circle[] cir) { //程序代码//其实就是在求数组中的最大值。 double max = cir[0].radius; for(int i = 1;i < cir.length;i++) { if(cir[i].radius > max) max = cir[i].radius; } return max; } } class Demo22 { public static void main(String[] args) { Circle cir[] = new Circle[3]; cir[0] = new Circle(1.0); cir[1] = new Circle(2.0); cir[2] = new Circle(4.0); System.out.println("最大的半径值是:"+Circle.compare(cir)); } }
/*22.写出程序结果*/ public class Demo { private static in a8d4 t j = 0; private static boolean methodB(int k) { j += k; return true; } public static void methodA(int i) { boolean b; b = i<10 | methodB(4); b = i<10 ||methodB(8); } public static void main(String[] args) { methodA(0); System.out.println(j); } } /* 打印结果为: 4 */
/* 第23题: 假如我们在开发一个系统时需要对员工进行建模, 员工包括3个属性,姓名,工号,以及工资。经理 也是员工,除了含有员工的属性外,另外还有一个 奖金属性。请使用继承的思想设计出员工类和经理类 ,要求类中提供必要的方法进行属性访问。 员工类:name id pay 经理类:继承了员工,并有自己特有的bonus */ abstract class Employee { private String name; private String id; private double pay; Employee(String name,String id,double pay) { this.name = name; this.id = id; this.pay = pay; } public abstract void work(); } class Manager extends Employee { private int bonus; Manager(String name,String id,double pay,int bonus) { super(name,id,pay); this.bonus = bonus; } public void work() { System.out.println("Manager work..."); } } class AbstactTest { public static void main(String[] args) { System.out.println("Hello World!"); } }
/* 第24题:在一个类中编写一个方法,这个方法搜索一个 字符数组中是否存在某个字符,如果存在,则返回 这个字符在字符数组中第一次出现的位置(序号从0开始计算) 否则,返回-1,要搜索的字符数组和字符都已参数 形式传递给该方法,如果传入的数组为null,应抛出 IllegalArgumentException异常, 在类的main方法中已各种可能出现的情况测试 验证该方法编写的是否正确, 例如:字符不存在,字符存在,存入的数组 为null等。 */ getIndex(null,'a'); public int getIndex(char[] arr,char key) { if(arr==null) throw new IllegalArgumentException("数组为null"); for(int x=0;x<arr.length;x++) { if(arr[x]==key) return x; } return -1; }
/* 25.补足函数内的代码,不许添加其他函数 */ class Circle { private double radius; public Circle(double r) { radius = r; } public Circle compare(Circle cir) { //程序代码 return (this.radius > cir.radius)?this:cir; } } class Demo23 { public static void main(String[] args) { Circle cir1 = new Circle(1.0); Circle cir2 = new Circle(2.0); Circle cir; cir = cir1.compare(cir2); if(cir1==cir) System.out.println("圆1的半径比较大"); else System.out.println("圆2的半径比较大"); } }
/* 总结有关包(package)的一些知识点: 包与包之间进行访问,被访问的包中的类以及 类中的成员,需要public修饰。 不同包中的子类还可以直接访问父类中被 protected修饰的成员。 包与包之间可以使用的权限只有两种: public protected public protected default private 同一个类中 ok ok ok ok 同一个包中 ok ok ok 子类 ok ok 不同包中 ok */
-----Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
相关文章推荐
- java 学习笔记(入门篇)_java的安装与配置
- #无私分享《传智播客2011版。net平台c#视频》全套哦30.7G
- 黑马程序员---Java面向对象编程思想小结
- 黑马程序员---java面向抽象/接口编程
- 黑马程序员---java单例设计模式
- 黑马程序员---java字符串常用类
- 黑马程序员宣传语征集大赛
- 黑马程序员-------关于今天的一个程序学习心得
- 黑马程序员----把Eclipse修改为黑色主题颜色方案
- c基础语法(一)---流程控制与函数
- C基础语法(二)---复杂数据类型(数组和字符串)
- C基础语法(三)---复杂数据类型(指针)
- OC基础语法(三)---核心语法(1)
- OC基础语法(四)---核心语法(2)
- OC基础语法(五)---内存管理
- OC基础语法(六)---内存管理(二)
- OC基础语法(七)---Foundation框架
- 黑马程序员
- 程序员常用英语大集合 http://bbs.itheima.com/thread-118950-1-1.html (出处: 黑马程序员IT技术论坛)
- IO(输入输出流) 4000