首页 > 移动平台 > 详细

Android Java 自定义异常

时间:2015-06-21 14:21:42      阅读:256      评论:0      收藏:0      [点我收藏+]

1、自定义异常

package com;

public class ZeroException extends Exception {

    private static final long serialVersionUID = 1L;

    public ZeroException(){
        super() ;
    }
    
    public ZeroException ( String string ) {
        super( string ) ;
    }
}

 

2、异常捕捉

package com;

public class Test {

    public static void main(String[] args) {
        Test test = new Test() ;
        
        try {
            test.fun( "" ) ;
        } catch (ZeroException e) {
            e.printStackTrace();
        } 
    }


    void fun( String string ) throws ZeroException {
        if( string == null ){
            throw new ZeroException( "参数不能为null" ) ;
        }

        if( string == "" ) {
            throw new ZeroException( "参数不能为空" ) ;
        }
    }
}

 

3、运行结果

com.ZeroException: 参数不能为空
    at com.Test.fun(Test.java:22)
    at com.Test.main(Test.java:9)


 

 

Android Java 自定义异常

原文:http://www.cnblogs.com/zhaoyanjun/p/4591903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!