zoukankan      html  css  js  c++  java
  • 自定义异常的使用

    自定义异常的简单使用,直接复制代码在一个class上,运行就可以了,根据注释看下去就明白了,简单的异常使用。

    package com.linewell.customException;
    
    /**
     * Linewell
     * @Detail:自定义异常的使用,直接在一个class上run as:Java Application就可以了
     * @Date:2017年2月9日
     * @author:wzelei
     */
    public class WzlException {
        /**
         * @Detail:主函数
         */
        public static void main(String[] args) {
     
            try {
                TestClass testClass = new TestClass();
                testClass.testException("123");//测试异常
            } catch (CustomException e) {
                e.printStackTrace();//输出打印异常的位置和类型
                System.out.println("异常型号: "+e.getWCode());
                System.out.println("异常信息: "+e.getWMessage());
            }
        }
    }
    /**
     * Linewell
     * @Detail:继承Exception,自定义异常类
     * @Date:2017年2月9日
     * @author:wzelei
     */
    class CustomException extends Exception{
    
        private static final long serialVersionUID = -4368280900457925605L;
    
        private String wCode;//异常类型代码
        private String wMessage;//异常类型信息
        
        public CustomException(String s,String d){
            this.wCode = s;
            this.wMessage = d;
        }
        public String getWCode(){
            return this.wCode;
        }
        public String getWMessage(){
            return this.wMessage;
        }
    }
    /**
     * Linewell
     * @Detail:抛出异常
     * @Date:2017年2月9日
     * @author:wzelei
     */
    class TestClass {
        /**
         * @Detail:根据参数不同抛出异常
         */
        public void testException(String s) throws CustomException {
            
            if(s.equals("123")){//判断异常类型,根据参数决定抛出不同的类型信息
                throw new CustomException("001", "A类型出错啦!");
            }else{
                throw new CustomException("002", "B类型出错啦!");
            } 
        }
    }
  • 相关阅读:
    numpy 加速 以及 ipython
    sublime 插件
    doc文件转txt
    xls文件转化txt
    jmlr论文下载
    R 画地图
    shell并行处理
    Idea 配置
    beta函数分布图
    winEdt 使用
  • 原文地址:https://www.cnblogs.com/wzlblog/p/6381321.html
Copyright © 2011-2022 走看看