A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Kingchen 中级黑马   /  2016-6-22 22:54  /  534 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

* A:异常处理的两种方式
        * a:try…catch…finally
                * try catch
                * try catch finally
                * try finally
        * b:throws
* B:try...catch处理异常的基本格式
        * try…catch…finally
* C:案例演示
        * try...catch的方式处理1个异常
案例:
  1. package com.heima.exception;
  2. public class Demo_Exception {
  3.         /**               
  4.                 try:用来检测异常的
  5.                 catch:用来捕获异常的
  6.                 finally:释放资源
  7.                
  8.                 世界上最真情的相依就是你在try我在catch,无论你发神马脾气,我都静静接受,默默处理
  9.                 当通过trycatch将问题处理了,程序会继续执行
  10.          */
  11.         public static void main(String[] args) {
  12.                 Demo2 d = new Demo2();
  13.                 try{
  14.                         int x = d.div(10, 0);
  15.                         System.out.println(x);
  16.                 }catch(ArithmeticException a) {                //ArithmeticException a = new ArithmeticException();
  17.                         System.out.println("出错了,除数为零了");
  18.                 }
  19.                
  20.                 System.out.println("1111111111111111");
  21.         }
  22. }
  23. class Demo2 {
  24.         /*
  25.          * 除法运算
  26.          */
  27.         public int div(int a,int b) {                //a = 10,b = 0
  28.                 return a / b;                                        // 10 / 0  被除数是10,除数是0当除数是0的时候违背了算数运算法则,抛出异常
  29.                                                                                 //new ArithmeticException("/ by zero");
  30.         }
  31. }
复制代码


1 个回复

正序浏览
这为知笔记上面的东西都搞出来了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马