尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

[Java] 异常

[Java] 异常 异常概念异常的概念异常程序运行过程中发生的特殊情况 / 错误状态会打断程序正常执行流程需要专门处理三个常见运行时异常1算术异常 ArithmeticException2数组越界异常 ArrayIndexOutOfBoundsException3空指针异常 NullPointerException异常体系Java 异常体系的根类是Throwable它下面分为两大分支Error和ExceptionThrowable1所有异常、错误的顶层父类只有它的子类才能被throw/catchError错误JVM 层面的严重问题程序无法处理代码不需要捕获典型例子1StackOverflowError栈溢出递归死循环2OutOfMemoryError内存溢出堆内存耗尽Exception异常程序员可处理分为两类受检异常编译期异常和非受检异常运行时异常 RuntimeException1受检异常Checked Exception编译时就会检查必须try-catch或者throws声明抛出a) IOExceptionIO 读写异常FileNotFoundException文件找不到IOException 子类b)ClassNotFoundException类找不到2RuntimeException 运行时异常非受检异常编译不报错运行时才抛出一般是代码逻辑错误不需要强制捕获a)NullPointerException空指针b)ArithmeticException算术异常例1/0c)ClassCastException类型转换异常异常分类编译时异常受检异常 Checked Exception1定义编译阶段编译器就会检查必须手动处理try-catch捕获 或者throws声明抛出否则代码编译不通过2来源Exception的子类但不是RuntimeException3常见编译时异常IOException、FileNotFoundException、ClassNotFoundException运行时异常非受检异常 Unchecked Exception1定义编译阶段编译器不检查代码可以正常编译生成 class 文件程序运行时 JVM 才抛出异常2来源RuntimeException以及它所有子类3常见例子aNullPointerException空指针异常bArrayIndexOutOfBoundsException数组下标越界cArithmeticException算术异常除 0dClassCastException类型强转异常重点1语法错误 ≠ 异常比如把System.out.println拼写错误写成system.out.println这是语法错误编译直接失败不属于异常2异常的前提代码编译成功生成 class 文件JVM 运行时发生的错误3区分口诀a编译时异常受检编译就报错必须处理大多是外部环境问题文件、IOb运行时异常非受检编译不报错运行崩大多是自己写代码粗心导致处理编译异常的方式1方式 1try-catchpublic class Person implements Cloneable { private String name; private String gender; private int age; public Person(String name, String gender, int age) { this.name name; this.gender gender; this.age age; } Override public Person clone() { try { return (Person)super.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } return null; } public String getName() { return name; } public void setName(String name) { this.name name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender gender; } public int getAge() { return age; } public void setAge(int age) { this.age age; } }public static void main(String[] args) { Person s1 new Person(张三,男,20); Person s2s1.clone(); System.out.println(原始对象s1.getName() s1.getGender() s1.getAge()); System.out.println(克隆对象s2.getName() s2.getGender() s2.getAge()); System.out.println(两个对象是否是同一个(s2s1)); //false }2方式 2throws 声明抛出异常public class Person implements Cloneable { private String name; private String gender; private int age; public Person(String name, String gender, int age) { this.name name; this.gender gender; this.age age; } Override public Person clone() throws CloneNotSupportedException { return (Person)super.clone(); } // Override // public Person clone() { // // try // { // return (Person)super.clone(); // }catch(CloneNotSupportedException e) // { // e.printStackTrace(); // } // return null; // } public String getName() { return name; } public void setName(String name) { this.name name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender gender; } public int getAge() { return age; } public void setAge(int age) { this.age age; } } public static void main (String[] args) throws CloneNotSupportedException{ Person s1 new Person(张三,男,20); Person s2s1.clone(); System.out.println(原始对象s1.getName() s1.getGender() s1.getAge()); System.out.println(克隆对象s2.getName() s2.getGender() s2.getAge()); System.out.println(两个对象是否是同一个(s2s1)); //false }考点1CloneNotSupportedException编译时异常受检 Checked Exception2super.clone()Object 原生 native 方法浅拷贝3必须实现标记接口Cloneable否则运行抛CloneNotSupportedException4Override重写 Object 的 clone 方法
返回列表