java自定义Exception

来源:互联网 发布:女包包网淘宝网 编辑:程序博客网 时间:2024/05/20 14:27

1,捕捉自定义异常

try{    xxxService.method() }catch( 自定义异常   ){    //异常处理}

2,method方法内部抛出异常

//当内部满足一定异常条件时:throw new MsgException(1002,"调用XX请求失败");

3,创建自己的 xxException 并继承 RuntimeException

=========================== demo 如下:=======================================
package com.sz.youban.dt.model;

public class MsgException extends RuntimeException
{
private static final long serialVersionUID = 1L;
private String msg;//自定义错误信息
private int code;//自定义错误码

public MsgException() {    super();}public MsgException(int code, String msg) {    this.code = code;    this.msg = msg;}public MsgException(String message) {    super(message);}public MsgException(Throwable cause) {    super(cause);}public String getMsg() {    return msg;}public void setMsg(String msg) {    this.msg = msg;}public int getCode() {    return code;}public void setCode(int code) {    this.code = code;}

}

原创粉丝点击