Regain the basics of Java (16): Summary of exceptions
1. Overview of exceptions
1. Exceptions are not equal to ordinary syntax errors. If there are missing semicolons or duplicate variable names, these are syntax errors and cannot be compiled.
2. An exception means that there is nothing wrong with the program code itself. The compilation can pass but problems will occur during runtime. This is called an exception. (But please note: If a method declares a non-RunTimeException exception through throws, then the exception needs to be handled when calling the method, otherwise the compilation will not pass. But if a method declares a RunTimeException exception through throws ,
3. Are there any exceptions?
## 4. The empty catch block itself can capture and handle exceptions, but for the convenience of debugging, some information related to the exception is generally output in the catch block
Benefits. : a. Easy to find problems b. How to collect program BUG more conveniently Software log
1. Five keywords are generally used to handle java exceptions: try catch finally throw throws
2. try{} catch(){} finally{} are generally used in combination, which means no additions are allowed in the middle. Any other code;
If there is no catch(){} between try{} and finally{}, the compilation will not be wrong, and the content in finally{} can also be executed, but it is meaningless. Note: When finally{} is used with return (return means ending the entire method), no matter how many returns there are in front,
##
3、try{ //把有可能产生异常的代码放到try代码块中监视起来 int[] arr={1,2,3}; System.out.println(arr[3]); // 自动new ArrayIndexOutOfBoundsException()并抛出,这是JVM做的 异常对象就是坏人 } catch(ArrayIndexOutOfBoundsException e){ //e用来存抛出来的异常对象,自动抓 (可以写代码也可以不写) } 4、try-catch的执行流程 A、try中发生了异常:从产生异常的那行代码直接跳到catch中 B、try中没有发生异常:会跳过catch往下执行 C、catch中的类型必须跟产生的异常对象的类型保证一致 注意:如果程序后面没有finally{} 也没有catch(Exception e){} catch中的类型不跟产生的异常对象的类型保持一致,程序会中断 5、多重catch块 A、一行代码又可能产生多种异常?有可能 B、一个catch一般只能捕捉一种异常,那么现在就需要写多个catch块 C、执行流程 D、要保证代码的可读性,因此不建议直接捕捉Exception,建议在最后捕捉Exception E、 jdk7提供了多重catch更简洁的写法,小括号中只能写平级的子类,不能写父类 注意catch中用的是“|”不是“||”。也不能写父类。 6、finally关键字 A、 该关键字要结合{} B、作用:无论前面发生了任何情况,他都能保证里面的代码肯定会被执行 C、一般把释放资源的代码放到finally块中 D、面试题:return 跟 finally 7、throws关键字 A、可以在方法内部进行异常处理 B、方法内部不做异常处理,而是在调用方法时进行异常处理 C、 怎么让调用者在调用方法时知道该处理哪些异常? throws用来把方法内可能产生的异常声明出来,告知调用者该处理哪些异常 D、细节 如果某方法通过throws声明了非RunTimeException的异常,那么在调用方法时必须进行异常处理,否则编译失败
throw关键字 1. 用来抛出自定义异常的对象 2. 由Java定义的异常,称之为标准异常;我们也可以自己定义一个异常类,称之为自定义异常 3. 如果控制台录入的性别既不是男也不是女,这也属于录入问题,但是Java中没有提供与之对应的异常类,我们就可以自定义这种异常类 4. 怎么定义一个异常类:直接继承Exception类 5. 怎么使用并抛出自定义异常类的对象 a.Java标准异常是由JVM自动创建并抛出的 b.自定义异常类需要我们自己创建并抛出package cn.itcast.demo;//SexException跟NullPointerException一样都是异常类 public class SexException extends Exception{ public SexException(){ super("性别异常"); }}package cn.itcast.demo;import java.util.Scanner;public class TestSexException{ public static void inputSex() throws SexException{ System.out.println("请输入你的性别:"); Scanner sc=new Scanner(System.in); String sex=sc.next(); if(!sex.equals("男")&&!sex.equals("女")){ throw new SexException(); } } public static void main(String[] args) { try { inputSex(); } catch (SexException e) { e.printStackTrace(); } }} 注意:try-catch-finally之间必须是连续的,不能夹杂其他代码
The above is about regaining the basics of java (16): summary of exceptions Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!