Java的检查异常处理

 

 

一、利用系统异常类

1、方法本身捕获异常并处理

 

try-catch/try-catch-finally

public class Exam4{
    public static void main(String args[]){
            Scanner sc=new Scanner(System.in);
            int i=sc.nextInt();
            System. out. println("输入的i为:"+i) ;
            try{ if( i==0){  System.out.println("没有异常!") ;}
                   if(i == 1){  int a=0;    int b=10;    b/=a;    }
                  if(i==2){  int m[]=new int[5];   m[5]= 100;    }
                  if(i==3){ String str="S6k9";   int n=Integer. parseInt(str);  }
             }
           catch(ArithmeticException  e){
                System.out.println("捕捉异常:"+ e ) ;}
          catch(ArrayIndexOutOfBoundsException e){
                System.out.println("捕捉异常:"+e) ;}
          catch(NumberFormatException e){
                System.out.println("捕捉异常:"+e) ;}
          finally//不管有没有异常都会执行
            { System.out.println("程序处理完毕!");}
 }}

 

 

 

2、方法本身抛出异常不处理,由调用此方法的方法处理,若都不处理则最终会抛给Java虚拟机

 

throw,throws

public class Exception {
    // throwOne方法后用throws声明异常类ArithmeticException  
static void throwOne(int i) throws ArithmeticException {  
	if(i==0)  
 	throw  new  ArithmeticException("i值为零"); //用throw抛出一个异常,不处理  
}  
public static void main(String  args[]){  
//捕获异常  
try
{  
	throwOne(0);  
 }  
catch(ArithmeticException e)
{  
	System.out.println("已捕获到异常错误: "+e.getMessage());  
}  
} 
}

throw与throws的区别:

 

 

    1、throw为程序员主动抛出的异常,一般与if一起用,可放在try语句块中,由catch捕捉并处理,如果在catch中用throw跑出异常则需要在本方法定义处用throws再将这个异常抛出去;

    2、throw用法:throw+new 类名([参数].....);即抛出一个异常对象;

 

    3、throws放在方法的头部,定义如下:

               访问权限修饰符  类型  方法名(参数表) throws  异常列表
    例如:
   public void  fun(int i) throws ArithmeticException, NumberFormatException{
             方法体;
     }

 

 二、自定义异常类

 

1、用户自定义的异常类,只需继承一个已有的异常类,包括继承Execption类及其子类,或者继承已自定义好的异常类。如果没有特别说明,可以直接用Execption类作为父类。

   自定义类的格式如下:

class   异常类名 extends  Exception
{
  ……
}

2、使用自定义异常的步骤如下:

(1)首先通过继承java.lang.Exception类声明自定义的异常类。

(2)在方法的声明部分用throws语句声明该方法可能抛出的异常。

(3)在方法体的适当位置创建自定义异常类的对象,并用throw语句将异常抛出。

 

(4)调用该方法时对可能产生的异常进行捕获,并处理异常。

例:a.定义

class MyException extends Exception
{
	private int detail;
	public MyException(int detail)
	{
		this.detail = detail;
	}
	
	public String toString()
	{
		return "MyException[" + detail + "]";  
	}
}  

    b.使用

import java.lang.Exception;
public class ExceptionDemo2 {
	static void compute(int a) throws MyException
	{
		System.out.println("调用compute("+a+")");
		if(a>10)
		{
			throw new MyException(a);
		}
		System.out.println("常规退出 ");
	}
	public static void main(String[] args)
	{
		try
		{	compute(1);
			compute(20);
		}catch(MyException e)
		{
			System.out.println("捕捉 "+e.toString());
		}
	}	
} 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值