zoukankan      html  css  js  c++  java
  • java异常处理机制

    java异常处理机制

    在整个Java的异常处理中,实际上也是按照面向对象的方式进行处理,处理步骤如下: 

    (1) 一旦产生异常,则首先会产生一个异常类的实例对象。 

    (2) 在try语句对此异常对象进行捕捉。 

    (3) 产生的异常对象与catch语句中的各个异常类型进行匹配,如果匹配成功,则执行catch语句中的代码。 

    Try{

     //产生异常代码,并产生异常类的实例对象    

           }catch(Exception e){ 

                 //匹配异常类型

                 //执行异常处理代码   

            }finally{  

                //异常统一出口  

    }  

    开发常见问题之一:程序开发中不会只存在一个异常,肯定会同时存在多个异常,此是,就需要使用多个catch语句进行处理,那捕获异常如何排顺?所以要注意,在Java中所有捕获范围小的异常必须放在捕获大的异常之前,否则程序编译时就会出现错误。

    代码:

    1. public class ExceptionDemo{  
    2.    public static void main(String args[]){  
    3.   
    4.       System.out.println("计算开始");  
    5.       int i=0 ;  
    6.       int j=0 ;  
    7.       try{  
    8.           String str1 = args[0] ;  
    9.           String str2 = args[1] ;  
    10.           i = Integer.parseInt(str1) ;  
    11.           j = Integer.parseInt(str2) ;    
    12.           int result = i /j ;          .......................产生异常  
    13.           System.out.println("两个数字相除结果:"+result) ;  
    14.       }catch(Exception e1){            ..................异常捕获范围大  
    15.            
    16.           System.out.println("其它异常:"+e1.getMessage()) ;           
    17.   
    18.       }catch(ArithmeticException e2){  ................错误,无法捕获  
    19.   
    20.           System.out.println("算术异常:"+e2.getMessage()) ;  
    21.   
    22.       }finally{  
    23.   
    24.           System.out.println("计算结果") ;  .............出口,一定执行  
    25.   
    26.       }  
    27.   
    28.    }  
    29. }  

    引用

    结果:编译出错。 



    引用
    开发常见问题之二:throw与throws区别。在定义一个方法时可以使用throws声明,使用throws声明的方法表示此方法不处理异常而交给方法调用处进行处理;而throw则直接抛出异常,两都常在一起使用,throw常用于自定义异常处理。上述为例说明 



    Java代码 
      1. public class ExceptionDemo{  
      2.     //执行相除  
      3.     public int div(int i,int j) throws Exception{.....往上抛,由方法调用处进行处理,这里则由主方法处理  
      4.         int result = 0 ;  
      5.         try{  
      6.   
      7.           result = i / j ;  
      8.   
      9.         }catch(Exception e){  
      10.   
      11.           throw e;                           ........直接抛出异常  
      12.   
      13.         }  
      14.         return result;  
      15.     }  
      16.     //主方法  
      17.     public static void main(String args[]){  
      18.           
      19.         ExceptionDemo ex = new ExceptionDemo();  
      20.     
      21.         try{  
      22.   
      23.            System.out.println("操作结果:"+ex.div(8,4));         
      24.   
      25.         }catch(Exception e){  
      26.      
      27.            System.out.println(e.getMessage()) ;  
      28.           
      29.         }  
      30.     }  
      31. }  
  • 相关阅读:
    OneThink友情链接插件使用!
    OneThink生成分类树方法(list_to_tree)使用!
    OneThink视图模型进行组合查询!文档组合文档详情
    Atitit.数据操作dsl 的设计 ---linq 方案
    Atitit.数据操作dsl 的设计 ---linq 方案
    Atitit.Atiposter 发帖机 信息发布器 v7 q516
    Atitit.Atiposter 发帖机 信息发布器 v7 q516
    Atitit.http连接合并组件   ConnReducerV3 新特性
    Atitit.http连接合并组件   ConnReducerV3 新特性
    Atitit.减少http请求数方案
  • 原文地址:https://www.cnblogs.com/mumuda/p/6134740.html
Copyright © 2011-2022 走看看