在实际的工作中,通常需要定义自己功能模块相关的异常,下面一个实例可以实现定义自己的异常:
1.定义一个异常码抽象类作为基类
public abstract class BaseExceptionCode { private String errorMessage ; private int errorCode ; private final BaseExceptionCode display ; public BaseExceptionCode(String errorMessage,int errorCode){ this.errorMessage = errorMessage; this.errorCode = errorCode ; this.display = this ; } public BaseExceptionCode(int errorCode,String errorMessage){ this.errorCode = errorCode ; this.errorMessage = errorMessage ; this.display = this ; } public String getErrorMessage(){ return errorMessage ; } public int getErrorCode(){ return errorCode ; } public BaseExceptionCode getDisplay(){ return display ; } }
2.定义自己的异常码
public class MyExceptionCode extends BaseExceptionCode{ protected MyExceptionCode(String errorMessage,int errorCode){ super(errorMessage,errorCode); } protected MyExceptionCode(int errorCode,String errorMessage){ super(errorCode,errorMessage); } public static final MyExceptionCode E_PARAM_IS_NULL_A = new MyExceptionCode(ConstantWithExceptionMsg.E_PARAM_NULL,ConstantWithExceptionCode.E_PARAM_CODE); public static final MyExceptionCode E_PARAM_IS_NULL_B = new MyExceptionCode(ConstantWithExceptionCode.E_PARAM_CODE,ConstantWithExceptionMsg.E_PARAM_NULL); }
3.定义自己的异常码集合(常量)
/** * 异常码集合 */ public class ConstantWithExceptionCode { public static final int E_PARAM_CODE = 100001 ; }
4.定义自己的异常信息集合(常量)
/** * 异常信息 */ public class ConstantWithExceptionMsg { public static final String E_PARAM_NULL = "参数为空" ; }
5.定义自己的异常
public class MyException extends RuntimeException{ private static final long serialVersionUID = -1933116620478022916L; private String errorMessage ; //异常信息 private MyExceptionCode errorCode ; //异常码 public MyException(MyExceptionCode errorCode){ super(errorCode.getErrorMessage()); this.errorCode = errorCode ; this.errorMessage = errorCode.getErrorMessage(); } public MyException(String errorMessage,MyExceptionCode errorCode){ super(errorMessage); this.errorMessage = errorMessage ; this.errorCode = errorCode ; } public MyException(MyExceptionCode errorCode,String errorMessage){ super(errorMessage); this.errorCode = errorCode; this.errorMessage = errorMessage; } public String getErrorMessage(){ return errorMessage ; } public MyExceptionCode getErrorCode(){ return errorCode ; } }
6.测试自己的异常
public class MyExceptionTest { public static void main(String[] args){ try{ test("",""); }catch(MyException e){ System.out.println(e); throw e ; } } private static void test(String str1,String str2) throws MyException{ if("".equals(str1) || str1 == null || "".equals(str2) || str2 == null ){ throw new MyException(MyExceptionCode.E_PARAM_IS_NULL_A); }else{ return ; } } }
7.测试结果
com.zh.exception.MyException: 参数为空
Exception in thread "main" com.zh.exception.MyException: 参数为空
at com.zh.exception.MyExceptionTest.test(MyExceptionTest.java:16)
at com.zh.exception.MyExceptionTest.main(MyExceptionTest.java:6)
相关推荐
java development part of exception
Copy Exception Exception Debug Event unionData GlobalSize Exception Exception 判断开始 Exception pExceptionRecord ExceptionCode = 十六到十 #Exception BreakPoint " int3断点 ExceptCount = ...
iOS , mac signal exception catch .
自定义异常类MyException,该类继承自Exception类,类中只有含一个字符串参数msg的构造方法,构造方法中只有一条语句super(msg)——调用父类的构造方法。另外,编写自定义类person,类中只有两个私有的变量,一个是...
java编写的OPCclient,实现与KEPServer模拟通讯,结合博客解释清晰。
catch(Exception e) { e.printStackTrace(); } } //带日期设置的构造函数 public JCalendar(int year, int month, int day) { cal.set(year, month, day); try { jbInit(); } catch (Exception e) { e....
package shiyan; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*;... } catch (Exception ee) { } } } else if (e.getSource() == cancelbtn) { dispose(); } } }
测试用例的辅导资料 非常适合做软件测试的初学者 好好学习 资源共享!!
* @throws Exception */ public WebServiceClientUtil(String server, int port, String path, String namespace, String prefix, long timeOutInMilliSeconds); gssByDataClient = new WebServiceClientUtil...
编写一个控制台版的租车程序。功能:1、展示所可以租的车辆2、选择车型、租车量3、展示租车清单
利用枚举来输出对客户端提示信息和异常码,以assert的形式编写抛出异常的代码! 例:SimpleResponseEnum.LICENCE_NOT_FOUND.assertNotNull(123); SimpleResponseEnum.SYSTEM_EXCEPTION.assertNotNull(null, "123");
catch (Exception e) { System.out.println(e); } } }; class process implements Runnable { Socket s; public process(Socket s1) { s=s1; } public void run() { String content=" "; try { ...
Java语言编写的汉诺塔程序,形象直观; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tower extends Frame implements ActionListener,Runnable { HannoiTower tower=null; ...
题目:输入两个整数将其整除,用try-catch语句捕获除数为0的异常。 本代码是c#语言编写的关于异常处理的代码,运行正常。
3.报表使用简单,功能强大,通过拖放等方式构建分析界面, 0代码编写。 4.开放源码,采用apache2.0开源协议,用户可任意使用而不需我公司授权(企业版除外)。 # 系统功能: 1.数据建模 (支持:mysql/...
在Exception的第一个描述中,它被认为是一个中断编写代码的正常执行的事件,不能在代码和编程环境中被称为错误或错误没有理由这样做。Exception跟踪器工具实际上是Exceptions的跟踪器和报告工具,其中包括其他调试...
编写一个方法参数接收一个字符串,返回一个Date对象(在多种日期格式中找到与字符串匹配的那一个)用到解析异常ParseException import java.text.ParseException; import java.text.SimpleDateFormat; import java....
java编写的计算器,能实现加减乘除。 //***该梦幻计算器与Windows附件自带计算器的标准版功能、界面相仿***// //******但还不支持键盘操作,如有需要,下一次等级将满足你**********// import java.awt.*; import ...
}catch(Exception ee){ text.setText("我正在读取"+url); } } public void run(){ try{ int n=-1; editPane.setText(null); url=new URL(text.getText().trim()); editPane.setPage(url); }catch...
封装一些提示信息(信息内容可以自己编写)。 需要将这个异常对象告知给调用者。怎么知道呢?怎么将这个异常对象传递到调用者呢?通过关键字throw就可以完成。throw异常对象。 throw用在方法内,用来引发一个异常...