异常类、自定义异常类的定义与使用,try{ }catch(){ }语句和throw关键字的执行流程

异常类的定义:

异常指:程序运行时可能出现的一些错误,比如试图打开一个根本不存在的文件等,异常处理将会改变程序的操作流程,让程序有机会对错误做出处理;

1、Java使用 throw 关键字抛出一个 异常类(Exception) 或者 异常类的子类 的实例,表示异常的发生;

2、异常对象可以调用如下方法得到或输出有关异常的信息;

public String getMessage();
public void printStackTrace();
public String toString();

3、Java使用 try{ }catch(){ } 语句来处理异常;


try{ }catch(){ } 语句:

1、将可能出现异常的操作放在 try{} 部分;

2、一旦 try{} 部分抛出异常对象 或 调用某个可能抛出异常对象的方法,那么 try{} 部分将立刻停止执行,转向执行相应catch部分;

3、程序将发生异常后的处理放在 catch(){} 部分;

try{ }catch(){ } 语句格式如下:

try{
   ...
 }catch(Exception e){
   ...
 } 

或者

try{
   ...
 }catch(ExceptionSubClass1 e){
   ...
 }catch(ExceptionSubClass2 e){
   ...
 } 

ExceptionSubClass1 和 ExceptionSubClass2 分别是继承了 Exception 类的子类,也就是 自定义异常类;


异常类 和 try{ }catch(){ } 语句 的使用:

通过 try{} 部分发生异常操作,由关键字 throw 抛出一个实例,发送给语句 catch(Exception e){ } 中的异常类对象 e,由 catch(Exception e){ } 的 { } 部分决定是否打印异常信息,并做出处理;

如下代码↗:

public class customizeExceptionTest {

	void method(int x,int y) throws Exception{
		int sum = x + y ;
		if(sum > 100 ){
			throw new Exception("错误,x+y>100");
		}else{
			System.out.println("x+y的值 = "+sum);
		}
	}
	
	public static void main(String [] args){
		customizeExceptionTest cet = new customizeExceptionTest();
		try{
			
			cet.method(11, 11);
			cet.method(10, 10);
			cet.method(100, 1);
			cet.method(1, 1);
		
		}catch(Exception e){
			System.out.println("打印异常信息:"+e.getMessage());
		}
		
	}
}

代码中,try{} 部分执行到 cet.method(100.1);时,就会发生异常,通过关键字 throw 进行抛出

异常的实例 newException("错误,x+y>100");  并转到catch(){ } 部分,对异常进行处理;

try{} 发生异常时,异常下面的语句将不再执行,如代码中的 cet.method(1, 1); 语句不会被执行;

代码输出结果:

x+y的值 = 22
x+y的值 = 20
打印异常信息:错误,x+y>100


自定义异常类的定义:

自定义异常类是异常类(Exception类)的一个子类,可以根据程序的需要来规定哪些方法产生哪些异常;

1、使用自定义异常类时,还需要用到Java另一个关键字 throws ;

2、关键字 throws 可以声明一个方法要发生的若干个异常,并在该方法的方法体中具体给出产生异常的操作;

产生异常的操作指:throw new Exception("异常信息");   //执行这条语句后,将会跳转到 catch(){ } 部分;

3、也就是:用相应的异常类创建对象,并使用 throw 关键字抛出该异常的实例,从而导致该方法结束执行;


自定义异常类的使用:

创建一个类,继承 Exception 类,并在类中创建异常处理方法;

如下代码↗:

//自定义异常类,继承Exception  
public class customizeExceptionClass extends Exception  {
	String message;
        //接收异常信息
	public customizeExceptionClass(String string) {
		message = string ;
	}
   //输出异常信息
    public String getMessage(){
    	return message;
    }
}
import java.util.Scanner;
//测试类
public class customizeException {
	public static void main(String[] args) {

	//从键盘输入一个数,判断这个数是否>100,是则抛出异常
	System.out.println("请输入一个数字,并按enter键结束输入:");
	Scanner sc = new Scanner(System.in);
	
        try{
        	double number = sc.nextDouble();

        	for(int i=3;i>0;i--){
        		
        		System.out.println("输入正确,输入次数还剩余:"+i+" 次");
        		
        		number = sc.nextDouble();
        	//通过if语句,判断这个数是否>100,是则抛出异常
        	     if(number > 100){
     		       throw new customizeExceptionClass("输入数值大于100");
     		      }
                 }
        
        }catch(customizeExceptionClass e){   
             System.out.println("异常处理,错误:"+e.getMessage());
        }
        //try{}catch(){} 语句结束
    }
}

在代码中,自定义了一个异常类customizeExceptionClass ,操作这个异常类对 try{} 部分发生异常时,catch(){ } 部分调用异常信息;

代码输出结果:

请输入一个数字,并按enter键结束输入:

12
输入正确,输入次数还剩余:3 次
14
输入正确,输入次数还剩余:2 次
155
异常处理,错误:输入数值大于100


try{ }catch(){ }语句和throw关键字的执行流程:

在以上代码中,先执行 try{} 部分的操作,当输入 155 时,条件 number > 100 成立,执行 throw new customizeExceptionClass("输入数值大于100");

抛出异常的实例 与 异常信息,然后跳转到catch(){ }部分,由 ()中的自定义异常类对象 e 接收异常的实例 与 异常的信息,

最后由 { } 部分进行打印异常信息 e.getMessage() ;


关键字 throws 的使用:

关键字 throws 可以声明一个方法要发生的若干个异常,并在该方法的方法体中具体给出产生异常的操作;

如下代码↗:

//自定义异常类
public class customizeExceptionClass extends Exception  {
	String message;
	//获取异常信息
	public customizeExceptionClass(String string) {
		message = string ;
	}
    //输出异常信息
    public String getMessage(){
    	return message;
    }
}
//创建可能发生异常的方法 method ,调用 throws 关键字表明产生的若干个异常
public class customizeExceptionTest {
 
	//throws的使用
	void method(int x,int y) throws customizeExceptionClass,Exception{
		int sum = x + y ;
		if(sum > 100 ){
			throw new customizeExceptionClass("错误,x+y>100");
		}else if(sum < 0){
			throw new Exception("错误,x+y<100");
		}else{
			System.out.println("正确,x+y的值 = "+sum);
		}
		
     }
}
//测试类,throws的使用
public class customizeException {

	public static void main(String[] args) {
	//throws的使用
	customizeExceptionTest cet = new customizeExceptionTest();
	try{
		
		cet.method(11, 11);
		cet.method(10, 10);
		cet.method(100,-111);
		cet.method(1, 1);
		
	}catch(customizeExceptionClass e){
		System.out.println("customizeExceptionClass打印异常信息:"+e.getMessage());
	}catch(Exception e){
		System.out.println("Exception打印异常信息:"+e.getMessage());
	}

   }
}

以上代码中,方法 method(int x,int y) 通过关键字 throws 表明可能产生的两个异常,当 try{} 部分操作中,有符合异常的条件时,就会抛出异常,转到异常相应的 catch(){} 语句中;

代码输出结果:

正确,x+y的值 = 22
正确,x+y的值 = 20
Exception打印异常信息:错误,x+y<100

将代码 cet.method(100,-111); 改为 cet.method(100,111); 时,就发生customizeExceptionClass 类异常,输出:

正确,x+y的值 = 22
正确,x+y的值 = 20
customizeExceptionClass打印异常信息:错误,x+y>100

发布了57 篇原创文章 · 获赞 10 · 访问量 7531

猜你喜欢

转载自blog.csdn.net/LagerSwan/article/details/104379395