免费网站建设软件,广州网站建设公司有哪些,做设计什么网站平台好点做私活,研发流程的六个阶段1.Throwsable 的两个子类 Exception 和 Error
2.Exception 这是编译期异常#xff0c;可以捕获处理
3.RuntimeException 表示运行期间的异常#xff0c;它是 Exception 的子类
4.Error 这是程序错误#xff0c;不可捕获处理。例如#xff0c;内存溢出
5.throws 关键字可以捕获处理
3.RuntimeException 表示运行期间的异常它是 Exception 的子类
4.Error 这是程序错误不可捕获处理。例如内存溢出
5.throws 关键字用于声明可能抛出编译异常让调用者处理异常。抛出的如果是运行时异常则可以不声明。方法内使用 throw 关键字抛出编译时异常就必须使用关键字 throws 在方法签名处声明可能抛出的异常
6.如果异常抛给 JVM 处理JVM 会打印异常信息然后直接停止程序
7.使用 try…catch 捕获异常后面的程序照常执行
运行期异常 NullPointerException ArrayIndexOutOfBoundsException继承自 IndexOutOfBoundsException
编译期异常 FileNotFoundException它继承自 IOException IOException它继承自 Exception
自定义异常类 继承自 Exception是编译时异常 继承自 RuntimeException是运行时异常无需处理
代码示例
public class Demo01Exception {public static void main(String[] args) {try {readFile(/users/home/test.txt);System.out.println(8888); // 捕获到异常此行代码不会执行} catch (IOException e) {e.printStackTrace();} finally {System.out.println(5555); // 有没有异常此行代码都会执行}System.out.println(9999); // 有没有异常此行代码都会执行}static void readFile(String path) throws IOException {if (!path.endsWith(.txt)) {throw new IOException(文件格式不对);}System.out.println(7777); // 抛出异常此行代码不会执行}public boolean findUserByAccountName(String accountName) {try {return true;} catch (Exception e) {e.printStackTrace();} finally { // 上面return后这行代码也会执行也就是说return后方法并没有立刻结束执行而是会继续找寻是否存在finally块存在则执行内部的代码执行完后方法才结束执行并将结果值返回给调用处System.out.println(关闭资源);}return false;
}}