异常初探(一)

来源:互联网 发布:女明星淘宝店铺大全 编辑:程序博客网 时间:2024/06/13 08:46
package com.liujunhua.it01;/** * 异常:就是程序运行过程中出现的不正常现象。 *  * 异常的由来:就是对程序运行过程中出现问题的描述, *  * 程序运行过程中会出现两类问题:通过error类描述的比较严重的错误,通过exception类描述的异常 *  * 对于exception可以进行针对性的处理 *  * 无论error或者exception  *  Throwable *    |---Error *    |---Exception *  * 异常的处理: try{需要被检测的代码} catch{处理异常的代码} finally{一定会执行的代码} *  *  */public class Demo03 {public static void main(String[] args) {ExceptionTest exceptionTest = new ExceptionTest();boolean mark = true;/** *经过对比发现,用try{}catch{}代码块处理异常后,不影响System.out.println("**********************");的执行 *但是如果不处理的话,System.out.println("**********************");将无法执行 */if (mark) {try {int x = exceptionTest.div(4, 0);System.out.println("x = " + x);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();//打印异常信息System.out.println(e.getMessage());}} else {int x = exceptionTest.div(4, 0);System.out.println("x = " + x);}System.out.println("**********************");}}class ExceptionTest {int div(int a, int b) {return a / b;}}

0 0
原创粉丝点击