🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## **Throw** throw:动词,真正的抛出一个异常对象 throws:形容词 ,形容方法的,表示某个方法可能抛出某个异常,要求方法者去调用它 ## **不处理,只抛出(可能会抛出异常)** ~~~ //不处理,只抛出(可能会抛出异常) public class Exception03 { public static void main(String[] args) throws FileNotFoundException { FileReader fr = new FileReader("1.txt"); } } ~~~ ### **2.捕获处理** ~~~ 语法格式 try { //可能出现异常的代码 } catch (FileNotFoundException e) { //异常处理 }finaily{ //写出必须哟啊执行的代码 //释放资源 } public class Exception03 { public static void main(String[] args) { try { FileReader fr = new FileReader("1.txt"); } catch (FileNotFoundException e) { e.printStackTrace(); } } //加上finally public class Exception03 { public static void main(String[] args) { FileReader fr =null; try { fr = new FileReader("1.txt"); int ch = fr.read(); } catch (java.lang.Exception ioe) { System.out.println("文件读取失败"); }finally { try { fr.close(); } catch (IOException e) { System.out.println("关闭文件失败"); } } } } ~~~ 3. * 多个异常,分别处理 * 一次捕获,多次处理 * 一次捕获,一次处理(常用)