java 异常处理

 

java中的异常一般都继承了Throwable类,它可以分为两个子类,一个是可以捕捉到异常的类Exeption,另一个是无法捕捉到异常的类Error。

主要研究的是可以捕捉到异常的类Exception,异常处理在很多时候都是很常见,特别是一些不确定或者容易出错的代码,这些代码可以通过编译,但运行阶段会出错。

 

一、常见捕捉异常

    public void Test1()
    {
        try
        {
            int num1=1;
            int num2=0;
            double result=num1/num2;    
            System.out.println(result);
            
        } catch (Exception e) 
        {
            System.out.println("除数为0"+e.getMessage());
        }
        finally {
            System.out.println("我不是英雄");
        }
    }

二、捕捉多个异常(多个catch)

    public void Test4()
    {
        try 
        {
            int num1=32;
            int num2=0;
            int result1=num1/num2;
            String[] strings=new String[5];
            strings[10]="Ambition";
        } 
        catch (ArithmeticException e)
        {
            System.out.println("除数为0:"+e.getMessage());
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界:"+e.getMessage());
        }
    }

三、多异常捕获(多个异常用 '|' 进行分割)

    public void Test1()
    {
        try 
        {
            int num1=2;
            int num2=3;
            double result=num1/num2;
            char[] ch=new char[5];
            ch[3]='W';
            System.out.println("我不是英雄");
        }
        catch ( IndexOutOfBoundsException|NumberFormatException|ArithmeticException e1)
        {
            e1.printStackTrace();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

四、异常finally回收资源

    public void Test3() throws Exception
    {
        Exception2 obj1=new Exception2("blue");
        Exception2 obj2=null;
        
        //对象输入流
        ObjectInputStream oInputStream=null;
        //对象输出流
        ObjectOutputStream outputStream=null;
        
        try 
        {
            //创建对象输入流
            oInputStream=new ObjectInputStream(new FileInputStream("Ambiton.bin"));
            //创建对象输出流
            outputStream=new ObjectOutputStream(new FileOutputStream("Confident.bin"));
            
            //序列化对象
            outputStream.writeObject(obj1);
            outputStream.flush();
            
            //反序列化对象
            obj2=(Exception2)oInputStream.readObject();
            
        }
        finally                           //使用finally回收资源
        {
            if(oInputStream!=null)
            {
                try 
                {
                    oInputStream.close();
                } 
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            
            if(outputStream!=null)
            {
                try 
                {
                    outputStream.close();
                }
                catch (Exception e) 
                {
                    e.printStackTrace();
                }
            }
        }    
    }

五、异常try()回收资源,其实使用try()方式是对finally的一种简化,系统自动实现了资源的回收。

    public void Test4() throws Exception
    {
        Exception2 obj1=new Exception2("blue");
        Exception2 obj2=null;
        
        try(
                //创建对象输入流
                ObjectInputStream oInputStream=new ObjectInputStream(new FileInputStream("Ambiton.bin"));
                //创建对象输出流
                ObjectOutputStream    outputStream=new ObjectOutputStream(new FileOutputStream("Confident.bin"));
                )
        {
            //序列化对象
            outputStream.writeObject(obj1);
            outputStream.flush();
            
            //反序列化对象
            obj2=(Exception2)oInputStream.readObject();

        }
    }

 

posted @ 2019-03-20 17:29  彩色的梦  阅读(169)  评论(0编辑  收藏  举报