LENOVO-PC_刘甲波12.17
异常
什么是异常?
异常,exception,是指程序运行过程中出现的非正常现象。
异常发生后怎么办?
发生异常要捕获
如果一个程序有异常发生,那么程序就无法正常运行了,会导致程序强行终止运行。当程序发生异常时,我们应该去捕获异常,只要异常被我们捕获,那么程序就没有异常了,由于没有异常了所以程序可以继续运行。
异常的体系结构(重要)
异常体系结构
Throwable类
throwable类是异常体系结构的父类。throwable类的父类是Object。在throwable下分两套异常类别,分别是Error和exception,其中Error是指错误,exception是指异常。
Error类
error类是无法通过开发人员编写程序能够解决的异常叫做Error。
典型的Error:内存溢出,动态链接失败(加载dll文件)、虚拟机错误
Exception类
Exception是所有异常类的父类,异常类是能通过编程人员编写程序处理的异常。Exception类有两套子类,一套是运行时异常,另一套是编译时异常。
规定:编译时异常必须捕获,如果不捕获会导致程序无法编译,也就无法运行了。
runtimeException类
runtimeException类是所有运行时异常的父类。
try,catch,finally异常 处理
用法1
try{
}catch(Exception e){
}
用法2
try{
}catch(Exception e){
}
finally{}
用法3
try{
}
finally{
}
解析:
try中用于包裹住可能要发生异常的程序
catch中用于捕获异常,并对异常进行处理
finally用于回收资源
try与catch组合时,一个try可以与多个catch组合,但是try只能有一个
try与finally组合时,一个try只能与一个finally组合,且try与finally都只能有一个。
try,catch,finally都不能单独使用,catch和finally都不能组合使用。
示例1:不捕获异常
package day;
public class Demo1 {
public static void main(String[] args) {
int i=1,j=0,res;
System.out.println("begin");
res=i/j;
System.out.println("end");
}
}
运算结果:
Exception in thread "main" begin
java.lang.ArithmeticException: / by zero
at day.Demo1.main(Demo1.java:7)
示例2:使用try和catch捕获异常
public class Demo1 {
public static void main(String[] args) {
try {
int i=1,j=0,res;
System.out.println("begin");
res=i/j;
System.out.println("end");
}catch(Exception e) {
System.out.println("catched");
e.printStackTrace();
}
System.out.println("over");
}
}
运行结果:
begin
catched
java.lang.ArithmeticException: / by zero
at day.Demo1.main(Demo1.java:8)
over
示例3:finally
public class Demo1 {
public static void main(String[] args) {
try {
int i=1,j=0,res;
System.out.println("begin");
res=i/j;
System.out.println("end");
}catch(Exception e) {
System.out.println("catched");
e.printStackTrace();
}
finally {
System.out.println("finally");
}
System.out.println("over");
}
}
运行结果:
begin
catched
java.lang.ArithmeticException: / by zero
at day.Demo1.main(Demo1.java:8)
finally
over
示例4:多重catch
import java.util.InputMismatchException;
public class Demo1 {
public static void main(String[] args) {
try {
int i=1,j=0,res;
System.out.println("begin");
res=i/j;
System.out.println("end");
}catch(InputMismatchException e) {
System.out.println("除数和被除数必须是整数");
}
catch(Exception e) {
System.out.println("catched");
e.printStackTrace();
}
finally {
System.out.println("finally");
}
System.out.println("over");
}
}
运行结果:
begin
catched
java.lang.ArithmeticException: / by zero
at day.Demo1.main(Demo1.java:10)
finally
over
throw,throws异常处理
throw和throws配合是一套处理异常的方式。
Thorws:用于定义在方法声明的后面,表示该方法 可能抛出异常,并告诉主调方法需要处理抛出的异常。Throws不能单独写一行代码,后面是抛出的异常类型,可以抛出多个异常类型,用逗号分隔开。
throw:用在方法内部,后面跟一个异常对象,表示要抛出这个异常。
用法1:被调方法不处理异常,直接抛给主调方法
在被调方法中如果发生了异常,被调方法不处理,告诉主调方法让主调方法来处理异常。
用法2:被调方法抓住异常后再次抛该主调方法(常用)
被调方法发生异常后被调方法要处理异常,被调方法处理异常后又抛出一个一异常给主调方法,告知主调方法让主调方法处理异常
自定义异常
当JDK中的异常类型满足程序的需要时,可以自定义异常类。使用自定义异常一般有一下几个步骤。
1.定义异常类,并继承Exception或者runtimeException
2.编写异常类的构造方法,向父类构造方法传入异常描述信息,并继承父类的其他实现方法
3.实例化自定义异常类对象,并在程序中使用throw抛出。
class GenderException extends Exception {
public GenderException(String message) {
super(message);
}
}
class Person{
private String name="";
private int age=0;
private String gender="男";
public void setGender(String gender)throws GenderException{
if("男".equals(gender)||"女".equals(gender)) {
this.gender=gender;
}else {
throw new GenderException("性别必须是男或女");
}
}
public void println() {
System.out.println("姓名:"+this.name+",性别"+this.gender+",年龄"+this.age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
}
public class Demo7{
public static void main(String[] args) {
Person person=new Person();
try {
person.setName("扈三娘 ");
person.setAge(18);
person.setGender("Female");
person.println();
}catch(GenderException e) {
e.printStackTrace();
}
}
}
异常相关的方法
getMesage():获取异常描述信息
printStackTrace();输出异常的堆栈信息
近期评论