java自定义栈类代码,异常堆栈和自定义类
本文概览:介绍了异常种类、异常堆栈和自定义异常。
1 异常种类
异常层次结构为:
对于Erro类【未检查异常】,描述的是系统内部错误类和资源耗尽错误。在代码中不抛出这种类型的对象,只需要关注Exception层次结构。
RuntimeExcetion【未检查异常】,就是运行时刻异常。我们在代码中自定义的异常都属于该类型。
IOException【已检查异常】。Throws只声明检查类型异常,即IOException类型;对于RuntimeException类型不声明。
2 堆栈
1、 堆栈信息理解
打印最上面的就是 最底层抛出异常的地方。举例如
main(){
f1();
}
f1(){
f2();
}
f2(){
f3()
}
1
2
3
4
5
6
7
8
9
main(){
f1();
}
f1(){
f2();
}
f2(){
f3()
}
如果执行main()时f3()有异常,则此时打印堆栈信息如下:
f3()异常
f2()位置
f1()位置
main()位置
2、堆栈信息的传递
public class TestExcetpion {
private void f1() {
throw new RuntimeException("f1");
}
private void f2() {
try {
f1();
} catch (Exception e) {
throw new RuntimeException("f2");
}
}
public void f3() {
try{
f2();
}catch (Exception e){
throw new RuntimeException("f3",e);}
}
public static void main(String[] args){
TestExcetpion e = new TestExcetpion();
e.f3();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
publicclassTestExcetpion{
privatevoidf1(){
thrownewRuntimeException("f1");
}
privatevoidf2(){
try{
f1();
}catch(Exceptione){
thrownewRuntimeException("f2");
}
}
publicvoidf3(){
try{
f2();
}catch(Exceptione){
thrownewRuntimeException("f3",e);}
}
publicstaticvoidmain(String[]args){
TestExcetpione=newTestExcetpion();
e.f3();
}
}
如下f3的堆栈信息只能到f2,不能延续到f1了。
Exception in thread “main” java.lang.RuntimeException: f3
at exception.TestExcetpion.f3(TestExcetpion.java:25)
at exception.TestExcetpion.main(TestExcetpion.java:31)
……..
Caused by: java.lang.RuntimeException: f2
at exception.TestExcetpion.f2(TestExcetpion.java:17)
at exception.TestExcetpion.f3(TestExcetpion.java:23)
… 6 more
(2)如果修改f3
public void f2() {
try{
f1();
}catch (Exception e){
throw new RuntimeException("f2",e);}
}
1
2
3
4
5
6
publicvoidf2(){
try{
f1();
}catch(Exceptione){
thrownewRuntimeException("f2",e);}
}
f3的堆栈信息才能向下延续到f1`
Exception in thread “main” java.lang.RuntimeException: f3
at exception.TestExcetpion.f3(TestExcetpion.java:23)
at exception.TestExcetpion.main(TestExcetpion.java:29)
……
Caused by: java.lang.RuntimeException: f2
at exception.TestExcetpion.f2(TestExcetpion.java:15)
at exception.TestExcetpion.f3(TestExcetpion.java:21)
… 6 more
Caused by: java.lang.RuntimeException: f1
at exception.TestExcetpion.f1(TestExcetpion.java:9)
at exception.TestExcetpion.f2(TestExcetpion.java:13)
… 7 more
3 自定义异常
1、自定义异常作用
是为了对于每一种异常都设置一个错误码。
2、自定义异常
public class MonitorException extends RuntimeException {
private String errorCode;
private String errorMsg;
private MonitorErrorEnum errorEnum;
public MonitorException() {
super();
}
public MonitorException(String errorCode, String errorMsg) {
super(errorCode);
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public MonitorErrorEnum getErrorEnum() {
return errorEnum;
}
public void setErrorEnum(MonitorErrorEnum errorEnum) {
this.errorEnum = errorEnum;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
publicclassMonitorExceptionextendsRuntimeException{
privateStringerrorCode;
privateStringerrorMsg;
privateMonitorErrorEnumerrorEnum;
publicMonitorException(){
super();
}
publicMonitorException(StringerrorCode,StringerrorMsg){
super(errorCode);
this.errorCode=errorCode;
this.errorMsg=errorMsg;
}
publicStringgetErrorCode(){
returnerrorCode;
}
publicvoidsetErrorCode(StringerrorCode){
this.errorCode=errorCode;
}
publicStringgetErrorMsg(){
returnerrorMsg;
}
publicvoidsetErrorMsg(StringerrorMsg){
this.errorMsg=errorMsg;
}
publicMonitorErrorEnumgetErrorEnum(){
returnerrorEnum;
}
publicvoidsetErrorEnum(MonitorErrorEnumerrorEnum){
this.errorEnum=errorEnum;
}
}
3、自定义错误码
public enum MonitorErrorEnum {
SUCESS("0", "成功"),
FAIL("9999", "失败");
private String errCode;
private String errMessage;
MonitorErrorEnum(String errCode, String errMessage) {
this.errCode = errCode;
this.errMessage = errMessage;
}
public String getErrCode() {
return errCode;
}
public void setErrCode(String errCode) {
this.errCode = errCode;
}
public String getErrMessage() {
return errMessage;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
publicenumMonitorErrorEnum{
SUCESS("0","成功"),
FAIL("9999","失败");
privateStringerrCode;
privateStringerrMessage;
MonitorErrorEnum(StringerrCode,StringerrMessage){
this.errCode=errCode;
this.errMessage=errMessage;
}
publicStringgetErrCode(){
returnerrCode;
}
publicvoidsetErrCode(StringerrCode){
this.errCode=errCode;
}
publicStringgetErrMessage(){
returnerrMessage;
}
publicvoidsetErrMessage(StringerrMessage){
this.errMessage=errMessage;
}
}
(全文完)
java自定义栈类代码,异常堆栈和自定义类相关推荐
- 登陆cf出现1.php,cf兄弟礼包验证码 一个PHP验证码类代码分享已封装成类
代码如下:<?php session_start(): Header("Content-type: image/gif"): class SecurityCode { pri ...
- JAVA之IO流、异常、File文件类
1 IO流 1.1IO流概述及其前奏 1.1.1IO流概述 IO流用来处理设备之间的数据传输,上传文件和下载文件 . 1.1.2IO流前奏 了解Io流前必须先了解异常和File类,因为File表示的是 ...
- pmd java规则_静态代码扫描 (一)——PMD 自定义规则入门
阅读该文章前,最好已经对 PMD 有了初步的认识和了解,可参考静态分析工具 PMD 使用说明 准备工作 首先在PMD 官网下载最新版本的文件,目前最新版本是 5.4.1. 下载 pmd-bin-5.4 ...
- java全栈系列之JavaSE-面向对象(类与对象的创建)032
类是抽象的一种数据类型,它是对某一类事物整体的描述和定义,但是类不能代表某一具体的事物 如:动物类,有猫.狗.兔子.这些类都是通过描述定义某一类具体的事物应该具备的特点和行为 注意:一个程序中只应该有 ...
- 二调建设用地地类代码_二调地类代码
0分 0 21.0KB 2017-09-17 认证考试aphza二调地类代码表. .~ ? 我们|打〈败〉了敌人. ?我们|[把敌人]打〈败〉了. 表A1 土地利用现状分类 一级类 二级类 含义 编码 ...
- java 日期类代码_java 日期时间处理类
import java.util.Calendar; import java.sql.Date; import java.text.SimpleDateFormat; import java.text ...
- php 上传 类 代码,php 文件上传类代码
/** * 文件上传类 */ class uploadFile { public $max_size = '1000000';//设置上传文件大小 public $file_name = 'date' ...
- java异常——异常分类+声明已检查异常+如何抛出异常+自定义异常类
[0]README 0.1) 本文描述+源代码均 转自 core java volume 1, 旨在理解 java异常--异常分类+声明已检查异常+如何抛出异常+自定义异常类 的相关知识: 0.2)异 ...
- java.lang.Exception 中常见异常的解释
一般面试中java Exception(runtimeException )是必会被问到的问题 常见的异常列出四五种,是基本要求.更多的....需要注意积累了 常见的几种如下: NullPointer ...
最新文章
- web服务器tornada,flask问题
- Flink1.4.0连接Kafka0.10.2时遇到的问题
- Word for mac 分小节问题
- Leetcode 剑指 Offer 03. 数组中重复的数字 (每日一题 20210614)
- 内容生态变现价值凸显,“长期主义者”触宝驶入快车道
- css中的node.js_在Node App中使用基本HTML,CSS和JavaScript
- Android Studio “Project Structure”选项目录结构显示异常
- [010]Try块和异常处理
- dell笔记本linux无网卡驱动,DELL R710 SERVER linux版本网卡驱动安装
- T19136 交通指挥系统 题解
- js正则表达式匹配多个条件
- (openCV 十二)图像增强(对数变换/伽马变换/分段线性变换)
- excel合并两列内容_Excel中如何跳过空单元格进行粘贴
- 大学物理(Ⅱ)公式整理
- 第一课 语言的发展史
- 软件缺陷报告与JIRA工具使用学习笔记
- 计算机维修要学英文吗,学计算机编程需要英文吗?
- Matlab实现曲线拟合的最小二乘法
- 衡量基因相对表达量的RPKM、FPKM、TPM详解
- 考研数学145分之路:暑期精读大学数学课本
热门文章
- 拿下微软、Google、Adobe,印度为何盛产科技圈 CEO?
- 马斯克又开始了,喋喋不休,吊足大家胃口
- 儿童手表还能这么用?定位功能防出轨 网友:这令人窒息的爱情
- 罗永浩确认12月初开发布会 不是手机也不是电子烟
- 哈啰单车失窃数十辆 盗窃者竟有摩拜员工!只因其又新又好骑...
- 上海查处一批涉“樱桃直播”传播淫秽物品牟利案女主播 已有14人获刑
- 编译安装sqlite-3.6.20【原创】
- java可变字符串替换字符,我们如何替换Java中String和StringBuffer的特定部分?
- Bitmap详解(上)常用概念和常用API
- 二叉树的应用 表达式处理_【每日编程208期】2018年408应用题41题