异常处理基础

1.异常概述与异常体系结构

1.1Java程序在执行过程中所发生的异常

分为两类:

<1>Error:Java虚拟机无法解决的严重问题,如:栈溢出(java.lang.StackOverflowError),堆溢出(java.lang.OutOfMemoryError);

<2>Exception:因编程错误或偶然的外在因素导致的一般性问题,如:空指针异常,网络连接中断,数组角标越界。主要讨论Exception。

1.2编译时异常和运行时异常

运行时异常(非受检异常):java.lang.RuntimeException类及它的子类都是运行时异常,不要求必须处理,一般为一些逻辑错误;

编译时异常(受检异常):是指编译器要求必须处理的异常,即程序运行时由于外界因素造成的一般性异常。编译器要求Java程序必须捕获或声明所有编译的异常。

1.3异常体系结构

1.4常见异常的举例

//******************以下是运行时异常***************************
//ArithmeticException
public void test1()
        {
        int a = 10;
        int b = 0;
        System.out.println(a / b);
        }

//InputMismatchException
public void test2()
        {
        Scanner scanner = new Scanner(System.in);
        int score = scanner.nextInt();
        System.out.println(score);
        scanner.close();
        }

//NumberFormatException
public void test3()
        {
        String str = "abc";
        int num = Integer.parseInt(str);
        }

//ClassCastException
public void test4()
        {
        Object obj = new Date();
        String str = (String)obj;
        }

//IndexOutOfBoundsException
public void test5()
        {
         //ArrayIndexOutOfBoundsException
            //int[] arr = new int[10];
            //System.out.println(arr[10]);
        //StringIndexOutOfBoundsException
        String str = "abc";
        System.out.println(str.charAt(3));
        }

//NullPointerException
public void test6()
        {
          //int[] arr = null;
         //	System.out.println(arr[3]);
        String str =null;
        System.out.println(str.charAt(0));
        }

        
//******************以下是编译时异常***************************
public void test7()
//      {
//		File file = new File("hello.txt");
//		FileInputStream fis = new FileInputStream(file);
//
//		int data = fis.read();
//		while(data != -1){
//			System.out.print((char)data);
//			data = fis.read();
//		}
//
//		fis.close();
//      }

2.异常处理的抓抛模型

2.1抓抛的过程

过程一:“抛”,是产生异常的过程。程序在执行过程中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象,并将此对象抛出,一旦抛出对象后,其后的代码就不再执行。关于“抛”的方式,有两种,一是系统自动生成的异常对象,二是手动生成的一个异常对象(throw)。

过程二:“抓”,是异常处理的过程。两种方法:1)try--catch--finally,2)throws

3.异常处理方式一——try-catch-finally

3.1形式

try
{
  //可能出现异常的代码
}
catch(异常类型1 变量名1)
{
  //处理异常的方式
}
catch(异常类型2 变量名2)
{
  //处理异常的方式
}
 .........
catch(异常类型n 变量名n)
{
  //处理异常的方式
}
finally //finall是可选的操作,不一定非要写出来
{
  //一定会执行的代码
}

3.2关于try-catch使用的一些说明

<1>finally是可选的操作;

<2>使用try将可能出现异常的代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配;

<3>一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理,一旦处理完成,就跳出当前的try-catch结构(在没写finall的情况下),继续执行后面的代码;

<4>catch中异常类型如果满足子父类关系,要求子类一定声明在父类的上面,否则报错;

<5>在try结构中声明的变量,在出了try结构后,就不能再被调用;

<6>try-catch-finally结构可以嵌套(比如catch中又有异常,接着在里面try);

<7>常用的异常对象处理的方式:1)getMessage(),获取异常信息,返回字符串;2)printStackTrace,获取异常类名和异常信息,以及异常出现在程序中位置,返回void。

简单举例:

public class Test1
{
    @Test
    public void test1()
    {
        int num1=0;
        String s1="12b";
        try
        {
            int num2 = Integer.parseInt(s1);
            num1=10;//上面一句出现异常了,这句没有执行,故最终的num1还是0
        }
        catch(NumberFormatException e)
        {
        //System.out.println(e.getMessage());
            e.printStackTrace();
        }
        //finally是可选的,这里没有写
        System.out.println(num1);
    }
}

3.3关于finally使用的一些说明

<1>finally中声明的是一定会被执行的代码,即使catch中又出异常了,try中有return语句,catch中有return语句等情况,也照样执行;

<2>像数据库连接、输入输出流、网络编程Socket等资源,JVM是不能自动的回收的,我们需要自己手动的进行资源的释放。此时的资源释放,就需要声明在finally中。

简单举例:

public class Test2
{
    @Test
    public void show()
    {
        int num=test2();
        System.out.println(num);
    }
    public int test2()
    {
        String s1="12b";
        try
        {
            int num = Integer.parseInt(s1);
            return 1;
        }
        catch(NumberFormatException e)
        {
            //System.out.println(e.getMessage());
            int []a=new int[10];
            int b=a[10];
            return 2;
            //e.printStackTrace();
        }
        finally
        {
            System.out.println("我肯定执行");
            return 3;
        }
    }
}

//输出结果为:
//我肯定执行
//3

4.异常处理方式二——throws

4.1throws的使用

<1>“throws+异常类型”写在方法的声明处,指明此方法在执行,可能会抛出的异常类型。一旦该方法执行后出现异常,会在异常代码出生成一个异常类的对象,若此对象满足throws后的异常类型时。就会被抛出,异常代码后面的代码,不会被执行。

<2>如果1)父类中被重写的方法,2)接口和抽象类中的抽象方法,没有throws方式异常处理,则子类(或实现类)重写的方法也不能使用throws,只能使用try-catch-finally处理。

<3>重写方法不能抛出比被重写方法范围更大的异常类型。

<4>简单举例: 

 5.手动抛出异常——throw

注意:throw一旦被执行,就抛出异常了

5.1代码举例

package exer3;

public class ReturnExceptionDemo
{
    static void methodA()
    {
        try
        {
            System.out.println("进入方法A");
            throw new RuntimeException("制造异常");
            //throw new RuntimeException("制造异常");等价于:
            //RuntimeException e=new RuntimeException("制造异常");
            //throw e;
        }
//如果像下面的注释代码这样写,就在这里catch了,不会进入主方法中的catch
//        catch (Exception e)
//        {
//            System.out.println("A中自己catch");
//        }
        finally
        {
            System.out.println("用A方法的finally");
        }
    }

    static void methodB()
    {
        try
        {
            System.out.println("进入方法B");
            return;
        }
        finally
        {
            System.out.println("调用B方法的finally");
        }
    }
    public static void main(String[] args)
    {
        try
        {
            methodA();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        methodB();
    }
}

//输出结果为:
//        进入方法A
//        用A方法的finally
//        制造异常
//        进入方法B
//        调用B方法的finally

5.2throw和throws的区别

throw:生成一个异常,并抛出,使用在方法内部,是一种手动抛出异常;

throws:是用来处理异常的,使用在方法声明处。

6.用户自定义异常类

6.1自定义异常类的要求

<1>做到见名知意;

<2>需要提供serialVersionUID;

<3>需要继承于现有的异常类:比如Exception,RuntimeException;

<4>需要定义重载的构造器;

<5>自定义的异常通过throw抛出。

6.2简单举例

对EcmDef.java,键入两个整数计算相除的结果,若存在负数,则返回EcDef异常(自定义异常)
public class EcDef extends Exception //继承现有的异常
{
    static final long serialVersionUID=12354465673463534L; //独有的serialVersionUID
    public EcDef(String msg) //重载的构造器
    {
        super(msg);
    }
}
import java.util.Scanner;

public class EcmDef
{
    public static double ecm (int i,int j) throws EcDef //抓住EcDef类型的异常
    {
        if(i<0||j<0)
            throw new EcDef("不可输入负数"); //输入负数,就抛一个EcDef类型的异常
        else
            return i/j;
    }

    public static void main(String[] args)
    {
        Scanner in=new Scanner(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        try
        {
            ecm(a,b);
        }
        catch (EcDef e)
        {
            System.out.println(e.getMessage());
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值