📄 java09.txt
字号:
安全的equals写法:-----------------------------------------------------------------------------------------public class Person { private int id; private String name; @Override public boolean equals(Object obj){ if(obj==null){ return false; }else if(obj.getClass() == this.getClass()){ //判断是不是本类类型,而用instanceof会将父类也认作同一个类中 Person p = (Person)obj; return this.id == p.id; } return false; } @Override public int hashCode(){ int temp = this.getClass().hashCode(); //直接得到本类的HashCode(),如果不是一个类一定不同 return temp; }}-----------------------------------------------------------------------------------------异常: 1.生成异常对象(JVM自动生成,程序员),包含异常信息 2.抛出异常,逐层上抛异常 3.找一个捕获异常的语句(try...catch) 4.如果抛到main方法还没有被捕获,那程序会终止算术异常(逐层上抛异常):-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class FirstException { public static void test(){ System.out.println("1"); int i = 3/0; System.out.println("2");//不打印 } public static void main(String[] args) { FirstException.test(); System.out.println("3");//不打印 }}输出结果:1Exception in thread "main" java.lang.ArithmeticException: / by zero at com.tarena.day09.exception.FirstException.test(FirstException.java:6) at com.tarena.day09.exception.FirstException.main(FirstException.java:10)-----------------------------------------------------------------------------------------常见异常: java.lang.ArithmeticException (算术异常) java.lang.NullPointerException (空指针异常) java.lang.ArrayIndexOutOfBoundsException (数组下标越界异常) java.lang.SecurityException (安全异常) java.lang.NegativeArraySizeException (数组长度为负异常) java.lang.ClassCastException (类型转换异常) java.lang.NumberFormatException (数字格式异常)try-catch-finally(捕获): try{ //only one 可能出现异常的代码 }catch(Exception e){ //0~n次 如果没有catch 一定要有finally 异常处理代码 }finally{ //0~1次 一定会执行的代码 }-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestTry { public static void test(){ try{ System.out.println("1"); int i = 3/0; //后边代码中断 System.out.println("2"); }catch (Exception ee){ System.out.println("出异常了"); ee.printStackTrace();//打印异常的堆栈信息 }finally{ System.out.println("3"); } } public static void main(String[] args) { test(); System.out.println("main"); }}输出结果:1出异常了java.lang.ArithmeticException: / by zero at com.tarena.day09.exception.TestTry.test(TestTry.java:7) at com.tarena.day09.exception.TestTry.main(TestTry.java:17)3main-----------------------------------------------------------------------------------------finally 中的语句一定会执行的:-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestTry { public static void test(){ try{ System.out.println("1"); int i = 3/1; //后边代码中断 System.out.println("2"); return; //即使有return也会执行下面的finally中的语句 }catch (Exception ee){ System.out.println("出异常了"); ee.printStackTrace();//打印异常的堆栈信息 return; //即使有return也会执行下面的finally中的语句 }finally{ System.out.println("3"); } } public static void main(String[] args) { test(); System.out.println("main"); }}输出结果:123main-----------------------------------------------------------------------------------------多catch:-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestTry { public static void test(){ try{ System.out.println("1"); int i = 3/0; //后边代码中断 System.out.println("2"); return; }catch (NullPointerException ee){ //如果有多个catch的时候一定要现跑出子异常再父异常,不能颠倒,但可以是平级的,根据异常的不同采取不同的处理方式 System.out.println("null"); }catch (Exception ee){ System.out.println("出异常了"); ee.printStackTrace();//打印异常的堆栈信息 return; }finally{ System.out.println("3"); } } public static void main(String[] args) { test(); System.out.println("main"); }}-----------------------------------------------------------------------------------------异常的时候catch 中一定要处理,不允许捕获异常而不处理,一旦输出结果出现异常,很有可能不知道有异常-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestTry { public static void test(){ try{ System.out.println("1"); int i = 3/0; //后边代码中断 System.out.println("2"); return; }catch (NullPointerException ee){ //如果有多个catch的时候一定要现跑出子异常再父异常,不能颠倒,可以根据异常的不同采取不同的处理方式 System.out.println("null");// }catch (Exception ee){// System.out.println("出异常了");// ee.printStackTrace();//打印异常的堆栈信息// return; }finally{ System.out.println("3"); } } public static void main(String[] args) { test(); System.out.println("main"); }}输出结果:13Exception in thread "main" java.lang.ArithmeticException: / by zero at com.tarena.day09.exception.TestTry.test(TestTry.java:7) at com.tarena.day09.exception.TestTry.main(TestTry.java:21)----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestFinally { public static int test(){ try { int i = 3/0; //无论是否出异常都会执行finally的语句 return 1; //遇到return的时候会先把return算出来,在这句执行之前会先执行finally } catch (Exception ee) { ee.printStackTrace(); return 2; }finally{ //finally肯定会屏蔽掉return 1,2,无论是否抛出异常 return 3; } } public static void main(String[] args) { System.out.println(test()); }}输出结果:java.lang.ArithmeticException: / by zero at com.tarena.day09.exception.TestFinally.test(TestFinally.java:6) at com.tarena.day09.exception.TestFinally.main(TestFinally.java:17)3-----------------------------------------------------------------------------------------矛盾转移,上抛异常throws 和 throw 的区别:throws 是用于方法后边的,代表方法抛出异常,把异常抛给调用者 (假异常,JVM不会做任何处理,只负责抛出异常,为了checkedExcpetion的编译通过,真正处理的时候还是要用try...catch ) 好处是能够让调用者根据实际需要灵活处理异常throw 后面跟一个异常对象,它是一个语句,用于程序员自己抛异常,不是由JVM抛异常-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class TestThrows { public static void test() throws Exception{//包括未检测异常,就必须要处理 throw new Exception("testexception"); } public static void test1() throws java.lang.NullPointerException{//因为是unchecked,所以不用处理 throw new NullPointerException("testexception"); } public static void main(String[] args) { test1(); try { test(); //必须处理 } catch (Exception e) { e.printStackTrace(); } }}-----------------------------------------------------------------------------------------静态类重写,抛异常:-----------------------------------------------------------------------------------------package com.tarena.day09.exception;import java.io.IOException;public class Super { public static void test() throws IOException{ //checked Exception System.out.println("super"); }}-----------------------------------------------------------------------------------------package com.tarena.day09.exception;import java.io.IOException;public class Sub extends Super{ public static void test() throws /*这里不能抛更大的异常*/IOException{ //静态方法应该用静态方法来覆盖 System.out.println("sub"); }//静态方法可以重写,但没有多态 public static void main(String[] args){ Super s = new Sub(); try{ s.test();//等于super.test() }catch(IOException e){ e.printStackTrace(); } }}输出结果:super-----------------------------------------------------------------------------------------自定义异常(不常用): 1.继承Exception或者是异常子类 2.提供无参,String作参数的构造 3.用throw 抛出自定义一个AgeException异常:-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class AgeException extends Exception{ public AgeException(){ super(); } public AgeException(String message){ super(message); }}-----------------------------------------------------------------------------------------package com.tarena.day09.exception;public class Person { private int age; public int getAge() { return age; } public void setAge(int age) throws AgeException {//throws时尽可能抛具体的异常 if(age<0 || age>150){ throw new AgeException("AgeError");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -