首页 技术 正文
技术 2022年11月23日
0 收藏 737 点赞 3,631 浏览 1386 个字

package com.gezhi;/**
 * 创建一个自定义异常SpendMoneyException类
 *
 * @author square 凉
 *
 */
@SuppressWarnings(“serial”)
/**
 * 该类继承异常类的父类Exception
 *
 * @author square 凉
 *
 */
public class SpendMoneyException extends Exception { /**
  * 显示写出自定义异常的无参构造器
  */
 public SpendMoneyException() { } /**
  * 创建一个自定义异常的有参构造器(重写父类的有参构造,严格来说不能是重写)
  *
  * @param message
  */
 public SpendMoneyException(String message) {
  super(message); }}………………………………………………………………………..package com.gezhi;
/**
 * @ 创建一个异常类
 * @author square 凉
 *
 */
public class ChainTest { /**
  * main方法调用test2这个方法,捕获并处理被抛出的方法
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub  ChainTest ct = new ChainTest();//实例化一个类对象
  try {
   ct.test2();//调用test2()这个方法
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();//捕获这个异常并打印异常发生的位置
  }
  
 } /**
  * 创建test2()这个方法引用test1的方法并捕获异常且不处理,继续抛给main方法
  */
 private void test2() {
  // TODO Auto-generated method stub
  try {
   test1();//调用test1方法
  } catch (SpendMoneyException e) {
   // TODO Auto-generated catch block
   RuntimeException rn = new RuntimeException(“一分钱一分货”);//将test1里面抛出的自定义异常又声明为运行时异常继续抛出
   rn.initCause(e);//引起该运行时异常的原因和异常地址
   throw rn;//抛出新的异常
  }
 } /**
  * test1方法声明一个自定义异常
  * @throws SpendMoneyExceptionSpendMoneyException不处理继续向下抛出
  */
 private static void test1() throws SpendMoneyException {
  // TODO Auto-generated method stub
  throw new SpendMoneyException(“没钱啦!!!”);
  
 }}………………….

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848