异常

常用结构

  try{
           //可能出现异常的语句     (try里捕获异常)
    }catch(异常类类名  e){  
           //处理异常的代码       (catch里执行异常)                                 
   }finally{
          //任何情况下都必须执行的代码
   }

注意:finally中的return语句总是会被执行,所以在开发中一般不将return语句放于finally中。

当使用

try {
		
	} catch (Exception e) {
		// TODO: handle exception
	}

结构处理异常时,Exception必须位于最后一个catch语句中(指未知错误)。
原因:
catch在匹配时从前往后匹配,父类型的异常可以捕获本类型+所有的子类型异常,所以子类的异常可以被父类型的异常捕获,后面定义的子类型的catch就没有意义。

Exception是父类,必须在并列分支的最后一句处理

所有异常的父类:Throwable

包含的方法:

public String getMessage()   //返回String类型的异常信息
public void printStackTrace()     //打印跟踪方法调用栈而获得的详细异常信息。

Throwable的子类*

Error类:表示仅靠程序本身无法恢复的严重错误,
Exception类:其子类分为俩大类
RuntimeException类:运行时异常,可避免,可处理也可不处理
非RuntimeException类:非运行时异常,java编译器会检查它,必须处理,否则编译不通过。

自定义异常类*:

用户自定义的异常继承 Exception类或是其子类,如果希望自己定义的运行时异常,则继承于RuntimeException类。
定义异常类时通常提供两个构造方法:
a. 一个是无参数的构造方法;
b. 另一个是带一个String类型参数的构造方法, 此字符串类型的参数作为该异常对象 的描述信息(即getMessage()方法的返回 值)。
例如:

public class Studet {
private String name;
private int age;
private String sex;
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 getSex() {
	return sex;
}
public void setSex(String sex)throws SetIOutRangeException{
	if (sex.equals(Sex.NAN)||sex.equals(Sex.NV)) {
		this.sex = sex;
	}
	else {
		throw new SetIOutRangeException("性别只能输入男或女");
	}
}
public Studet(String name){
	this.name=name;
	
} 
public class Sex {
public final static String  NV="女";
public final static String  NAN="男";
}
public class SetIOutRangeException extends RuntimeException{
public SetIOutRangeException(String msg){
	super(msg);
}
}

对于在子类、父类中的方法抛出的异常要求

注意:子类中的方法,不允许抛出比父类更多、更宽的异常。
例如:

public class Pet {
public void say() throws FileNotFoundException{
}
}
public class Dog extends Pet {
public void say()throws FileNotFoundException,EOFException {//报错,子类中的方法,不允许抛出比父类更多、更宽的异常。
	
}
}

父类中的成员方法抛出异常,throws Exception;子类重写父类的方法,可以不抛出异常;
例如:

public class Pet {
public void say() throws Exception{
}
}
public class Dog extends Pet {
public void say() {//子类重写父类的方法,可以不抛出异常;
}
}

父类中的构造函数抛出异常,throws Exception; 子类的构造函数,也必须抛出异常throws Exception;

public class Pet {
public Pet()throws Exception{
}
}
public class Dog extends Pet {
public Dog() throws Exception{	//父类中的构造函数抛出异常,throws Exception; 子类的构造函数,也必须抛出异常throws Exception;
}
}

如果父类的构造函数,抛出的是RuntimeErrorException ,则子类的构造函数可以不处理;

public class Pet {
public Pet()throws RuntimeException{
	
}
}
public class Dog extends Pet {
public Dog(){//父类的构造函数,抛出的是RuntimeErrorException ,则子类的构造函数可以不处理
	
}
}

常见的运行时异常:

数组下标越界异常:java.lang.ArrayIndexOutOfBoundsException
例如:

public static void main(String[] args) {
	int []a=new int[3];
	for(int i=0;i<=a.length;i++) {
		a[i]=1;
		a[i]++;
	}
}

类型转换异常:java.lang.ClassCastException
例如:Dog和Cat类都继承Pet类

public class Pet {
}
public class Dog extends Pet {
}
public class Cat extends Pet {
}
public static void main(String[] args){
	Pet pet=new Pet();
	Dog dog=new Dog();
	Cat cat=new Cat();
	pet=(Dog)pet;//异常,父类没有办法转为子类
}

空指针异常: java.lang.NullPointerException
例如:

public class Pet {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
public void say() {}
}
public void say() {
	System.out.println("小狗汪汪叫");
}
public class Cat extends Pet {

	public  void say() {
		System.out.println("小猫喵喵叫");
	}
}
public static void main(String[] args) {
	Pet pet=null;
	pet.say();//异常
}

数据类v型转换异常: java.lang.NumberFormatException
例如:

public static void main(String[] args){
	String   a="hello";	 
    int b=Integer.parseInt(a);
	System.out.println(b);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值