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)


 

 

相关文章:

  • 2021-12-24
  • 2020-06-04
  • 2022-03-08
  • 2022-02-10
  • 2022-02-10
猜你喜欢
  • 2021-06-22
  • 2021-10-24
  • 2022-01-01
  • 2021-12-16
  • 2021-06-01
  • 2021-09-23
相关资源
相似解决方案