zoukankan      html  css  js  c++  java
  • 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();
    
            }
        }
  • 相关阅读:
    html中frameset的详细使用方法
    日期控件API
    限制input输入类型(多种方法实现)
    springmvc导出excel并弹出下载框
    Spring mvc 验证码的做法
    Spring Boot 集成MyBatis
    Spring Boot 实践折腾记(三):三板斧,Spring Boot下使用Mybatis
    支付系统架构
    javaScript事件(六)事件类型之滚轮事件
    你不是真正的快乐
  • 原文地址:https://www.cnblogs.com/Optimism/p/10566309.html
Copyright © 2011-2022 走看看