首页 技术 正文
技术 2022年11月8日
0 收藏 972 点赞 2,087 浏览 1190 个字
  1. /*下面做了归纳总结,欢迎批评指正*/
  2. /*自定义异常*/
  3. class ChushulingException extends Exception
  4. {
  5. public ChushulingException(String msg)
  6. {
  7. super(msg);
  8. }
  9. }
  10. class ChushufuException extends Exception
  11. {
  12. public ChushufuException(String msg)
  13. {
  14. super(msg);
  15. }
  16. }
  17. /*自定义异常 End*/
  18. class Numbertest
  19. {
  20. public int shang(int x,int y) throws ChushulingException,ChushufuException
  21. {
  22. if(y<0)
  23. {
  24. throw new ChushufuException(“您输入的是”+y+”,规定除数不能为负数!”);//抛出异常
  25. }
  26. if(y==0)
  27. {
  28. throw new ChushulingException(“您输入的是”+y+”,除数不能为0!”);
  29. }
  30. int m=x/y;
  31. return m;
  32. }
  33. }
  34. class Rt001
  35. {
  36. public static void main(String[]args)
  37. {
  38. Numbertest n=new Numbertest();
  39. //捕获异常
  40. try
  41. {
  42. System.out.println(“商=”+n.shang(1,-3));
  43. }
  44. catch(ChushulingException yc)
  45. {
  46. System.out.println(yc.getMessage());
  47. yc.printStackTrace();
  48. }
  49. catch(ChushufuException yx)
  50. {
  51. System.out.println(yx.getMessage());
  52. yx.printStackTrace();
  53. }
  54. catch(Exception y)
  55. {
  56. System.out.println(y.getMessage());
  57. y.printStackTrace();
  58. }
  59. finally{ System.out.println(“finally!”);} ////finally不管发没发生异常都会被执行
  60. }
  61. }
  62. /*
  63. [总结]
  64. 1.自定义异常:
  65. class 异常类名 extends Exception
  66. {
  67. public 异常类名(String msg)
  68. {
  69. super(msg);
  70. }
  71. }
  72. 2.标识可能抛出的异常:
  73. throws 异常类名1,异常类名2
  74. 3.捕获异常:
  75. try{}
  76. catch(异常类名 y){}
  77. catch(异常类名 y){}
  78. 4.方法解释
  79. getMessage() //输出异常的信息
  80. printStackTrace() //输出导致异常更为详细的信息
  81. */

出处:http://blog.csdn.net/stellaah/article/details/6738424

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