java 异常处理

    xiaoxiao2026-03-06  8

    java中的所有不正常的累都继承于Throwable , Throwable 主要分为 Error 和 ExceptionError 中包括虚拟机错误和线程死锁 , 一旦Error出现 , 程序就彻底挂了 –程序终结者 -Exception 主要指编码、环境、用户操作输入 出现问题 , 主要包括 非检查异常(RuntimeException)和检查异常(其他的一些异常)RuntimeException 主要包括:空指针 ,数组下标越界,类型转换异常,算术异常

    异常处理 try-catch 以及 try-catch-finally try{ //一些会抛出异常的方法 }catch(Exception e){ //处理该异常的代码块 }catch(Exception2 e){ //处理Exception2的代码块 }…(n个catch块)…{ }finally{ //最终将要执行的一些代码 } Exception顺序问题 先子类后父类

    package cn.beepower.ning; /** * Created by NingDaolong on 2016/8/16. */ public class Test { public static void main(String[] args) { Test test = new Test(); int tre = test.tre(); System.out.println("tre = " + tre); } /** * @return */ public int tre() { int di = 10; int res = 100; try { while (di > -1) { di--; res = res + 100 / di; } return res; } catch (ArithmeticException e) { System.out.println("算术异常"); e.printStackTrace(); return -1; } catch (Exception e) { System.out.println("异常"); e.printStackTrace(); return -2; } } } 向上抛出异常自定义异常

    总结 -转载简易扑克牌 http://blog.csdn.net/qq_31028891/article/details/52122980

    转载请注明原文地址: https://ju.6miu.com/read-1307680.html
    最新回复(0)