如何创建一个可以使用try.....catch.......捕获的异常
2024-09-07 03:03:54
代码很简单,大家一看基本上就能明白(有一定的java基础,熟悉try......catch.....finally的使用方法)
package com.nokia.test1; public class test { public static void main(String[] args) { NumberTest n = new NumberTest(); //捕获异常
try{
System.out.println("商="+n.div(1,0)); }catch(customException1 yc){
System.out.println(yc.getMessage());
yc.printStackTrace();
}
catch(customException2 yx)
{
System.out.println(yx.getMessage());
yx.printStackTrace();
}
catch(Exception y)
{
System.out.println(y.getMessage());
y.printStackTrace();
} finally{ System.out.println("finally!");} ////finally不管发没发生异常都会被执行 } } class customException1 extends Exception{ public customException1(String msg) {
super(msg);
// TODO Auto-generated constructor stub
}
} class customException2 extends Exception{ public customException2(String message) {
super(message);
// TODO Auto-generated constructor stub
}
} class NumberTest{ public int div(int x, int y) throws customException1, customException2 { if (y<0) { throw new customException1("您输入的是"+y+",规定除数不能为负数!"); //抛出异常
} if(y==0)
{
throw new customException2("您输入的是"+y+",除数不能为0!");
} return x / y;
} }
如有什么好的建议,请畅所欲言!!!!!
参考链接:http://blog.csdn.net/stellaah/article/details/6738424
最新文章
- Spring Enable annotation – writing a custom Enable annotation
- yum 操作复习
- Trace1:Default Trace
- 第10章 同步设备I/O和异步设备I/O(1)_常见设备及CreateFile函数
- android studio 编译加速
- magento去掉add to cmpre和email to friend
- Python学习总结6:字符串格式化操作及方法总结
- EL表达式复习
- 帝国cms7.0忘记后台管理账户用户名密码
- linux上安装Drupal
- linux的学习系列 4---文件权限和访问模式
- 浅谈访问控制列表(ACL)
- 用js来实现那些数据结构08(链表02-双向链表)
- APP下载在微信无法打开 该如何处理
- java排序 冒泡?+插入排序
- css 获取从第n个开始,之后的所有元素
- <;Yarn>; <;Capacity Scheduler>; <;Source Code>;
- JAVA软件安装
- VM虚拟机装centos无法自动获取IP的解决方法
- js-ES6学习笔记-Class(3)
热门文章
- Android 数据存取
- (转载) 百度地图工具类封装(包括定位,附近、城市、范围poi检索,反地理编码)
- thread.h
- SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用
- [洛谷P3948]数据结构
- H3C S5130交换机堆叠操作
- 如何配置任意目录下Web应用程序
- 【codeforces 128C】Games with Rectangle
- hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)
- 在PyCharm中以root权限运行和调试python代码