自定义异常类一

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 自定义异常类 --->extends Exception--->extends Throwable都一样   异常类 1 package com.yeepay.sxf.exception; 2 /** 3 * 验证数据格式异常类 4 * @author sxf 5...

自定义异常类

--->extends Exception
--->extends Throwable
都一样

 

异常类

 1 package com.yeepay.sxf.exception;
 2 /**
 3  * 验证数据格式异常类
 4  * @author sxf
 5  *
 6  */
 7 public class VoaliteDataSimpleException extends Exception{
 8 
 9     /**
10      * 空构造
11      * e.printStackTrace()==>会打印:报错日志信息(类,方法名,行数)
12      * e.getMessage()==>打印:null
13      */
14     public VoaliteDataSimpleException(){
15         super();
16     }
17     
18     /**
19      * 第一种带参数构造
20      * e.printStackTrace()==>会打印:你输入的信息,报错的日志信息(类,方法名,行数)
21      * e.getMessage()==>仅打印:你输入的信息
22      * @param msg
23      */
24     public VoaliteDataSimpleException(String msg){
25         super(msg);
26     }
27     
28     /**
29      * 第二种构造
30      * @param msg
31      * @param cause
32      * e.printStackTrace()==>会打印:你输入的信息,报错的日志信息(类,方法名,行数),Caused By 内容
33      * e.getMessage()==>仅打印:你输入的信息
34      */
35     public VoaliteDataSimpleException(String msg,Throwable cause){
36         super(msg, cause);
37     }
38     
39     /**
40      * 第三种构造
41      * @param cause
42      *  e.printStackTrace()==>会打印:报错的异常类的全路径:你输入的信息,报错的日志信息(类,方法名,行数),Caused By 内容
43      * e.getMessage()==>仅打印:报错的异常类的全路径:你输入的信息
44      */
45     public VoaliteDataSimpleException(Throwable cause){
46         super(cause);
47     }
48 }
View Code

测试类

 1 package com.yeepay.sxf.exception;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         try {
 7             test01();
 8         } catch (VoaliteDataSimpleException e) {
 9             // TODO Auto-generated catch block
10             System.out.println("Test.main()"+e.getMessage());
11             System.out.println("Test.main(ssssssssssssssssssssssssssssssssssssssssssss)");
12             e.printStackTrace();
13         }
14     }
15     public static void test01() throws VoaliteDataSimpleException{
16         int a=0; 
17         int b=2;
18         if(true){
19             throw new VoaliteDataSimpleException(new VoaliteDataSimpleException("数据验证异常"));
20         }
21         
22     }
23 }
View Code

测试结果

Test.main()com.yeepay.sxf.exception.VoaliteDataSimpleException: 数据验证异常
Test.main(ssssssssssssssssssssssssssssssssssssssssssss)
com.yeepay.sxf.exception.VoaliteDataSimpleException: com.yeepay.sxf.exception.VoaliteDataSimpleException: 数据验证异常
    at com.yeepay.sxf.exception.Test.test01(Test.java:21)
    at com.yeepay.sxf.exception.Test.main(Test.java:7)
Caused by: com.yeepay.sxf.exception.VoaliteDataSimpleException: 数据验证异常
    ... 2 more

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
打赏
0
0
0
0
11
分享
相关文章
65 C++ - 自定义异常
65 C++ - 自定义异常
60 0
|
4月前
自定义异常
自定义异常类: public class InsufficientFundsException extends Exception { public InsufficientFundsException(String message) { super(message); } } 使用自定义异常: public void withdraw(double amount) throws InsufficientFundsException { if (balance < amount) { throw new InsufficientFundsExcepti
项目使用 GlobalExceptionHandler 与 @RestControllerAdvice自定义异常 二
项目使用 GlobalExceptionHandler 与 @RestControllerAdvice自定义异常 二
77 3
项目使用 GlobalExceptionHandler 自定义异常 一
项目使用 GlobalExceptionHandler 自定义异常 一
150 3
|
10月前
|
自定义异常类
自定义异常类
54 0
如何自定义异常类
如何自定义异常类
62 0
|
10月前
|
java异常处理,如何自定义异常?
java异常处理,如何自定义异常?
126 4
异常的处理和自定义异常
在Java中,异常(Exception)是指程序在运行过程中遇到的错误或异常情况。Java提供了异常处理机制,可以捕获和处理异常,使程序在遇到异常时能够继续执行或进行相应的处理。 异常处理的基本结构是try-catch语句块。在try块中编写可能抛出异常的代码,如果在try块中抛出了异常,那么程序会跳转到catch块中执行相应的异常处理代码。 以下是一个简单的异常处理的示例: ```java try { // 可能抛出异常的代码 int result = 10 / 0; } catch (ArithmeticException e) { // 异常处理代码
95 0
异常处理二:throws +异常类型
异常处理二:throws +异常类型
68 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等