代码很简单,大家一看基本上就能明白(有一定的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

最新文章

  1. Spring Enable annotation – writing a custom Enable annotation
  2. yum 操作复习
  3. Trace1:Default Trace
  4. 第10章 同步设备I/O和异步设备I/O(1)_常见设备及CreateFile函数
  5. android studio 编译加速
  6. magento去掉add to cmpre和email to friend
  7. Python学习总结6:字符串格式化操作及方法总结
  8. EL表达式复习
  9. 帝国cms7.0忘记后台管理账户用户名密码
  10. linux上安装Drupal
  11. linux的学习系列 4---文件权限和访问模式
  12. 浅谈访问控制列表(ACL)
  13. 用js来实现那些数据结构08(链表02-双向链表)
  14. APP下载在微信无法打开 该如何处理
  15. java排序 冒泡?+插入排序
  16. css 获取从第n个开始,之后的所有元素
  17. &lt;Yarn&gt; &lt;Capacity Scheduler&gt; &lt;Source Code&gt;
  18. JAVA软件安装
  19. VM虚拟机装centos无法自动获取IP的解决方法
  20. js-ES6学习笔记-Class(3)

热门文章

  1. Android 数据存取
  2. (转载) 百度地图工具类封装(包括定位,附近、城市、范围poi检索,反地理编码)
  3. thread.h
  4. SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用
  5. [洛谷P3948]数据结构
  6. H3C S5130交换机堆叠操作
  7. 如何配置任意目录下Web应用程序
  8. 【codeforces 128C】Games with Rectangle
  9. hdu4405--Aeroplane chess(概率dp第七弹:飞行棋游戏--2012年网络赛)
  10. 在PyCharm中以root权限运行和调试python代码