一、异常的体系架构

  •   java.lang.Throwable-----java.lang.Error:一般不编写针对性的代码进行处理。(此处不进行举例)-----java.lang.Exception:可以进行异常的处理
    
  •      编译时异常:(checked)io.IOExeptionFileNotFoundExceptionClassNotFoundExceptionFileNotFoundExceptionSQLExceptio
    
  •      运行时异常:(unchecked,RuntimeException)ClassCastExceptionArrayIndexOutOfBoundsExceptionNullPointerExceptionArithmeticExceptionNumberFormatExceptionInputMismatchException
    
package com.atguigu.java;import java.io.File;
import java.io.FileInputStream;
import java.sql.Date;
import java.util.Scanner;import org.junit.Test;public class ExceptionTest {//******************以下是编译时异常***************************//FileNotFoundException
//  @Test
//  public void test7(){//      File file = new File("hello.txt");
//      FileInputStream fis = new FileInputStream(file);
//
//      int data = fis.read();
//      while(data != -1){//          System.out.print((char)data);
//          data = fis.read();
//      }
//
//      fis.close();
//
//  }//******************以下是运行时异常***************************//ArithmeticException@Testpublic void test6(){int a = 10;int b = 0;System.out.println(a / b);}//InputMismatchException@Testpublic void test5(){Scanner scanner = new Scanner(System.in);int score = scanner.nextInt();System.out.println(score);scanner.close();}//NumberFormatException@Testpublic void test4(){String str = "123";str = "abc";int num = Integer.parseInt(str);}//ClassCastException@Testpublic void test3(){Object obj = new Date(0);String str = (String)obj;}//IndexOutOfBoundsException@Testpublic void test2() {ArrayIndexOutOfBoundsExceptionint[] arr1 = new int[10];System.out.println(arr1[10]);//java.lang.ArrayIndexOutOfBoundsException: 10String str = "abc";System.out.println(str.charAt(3));}//NullPointerException@Testpublic void test1() {int[] arr = null;System.out.println(arr[3]);}}

二、 异常处理:抓抛模型

  •  过程一:"抛":程序在正常执行的过程中,一旦出现异常,就会在代码处,生成一个对应异常类的对象将此对象抛出。一旦抛出对象后,其后代码就不再执行了(try里的)关于异常对象的产生:①系统自动生成的异常对象②手动生成一个异常对象,并抛出(throw)
    
  •  过程二:"抓":可以理解为异常的处理方式:①try-catch-finally ②throws
    

异常处理的方式一:try-catch-finally

  •  try{//可能出现异常的代码}catch(异常类类型1 变量名1){//处理异常的方式1}catch(异常类类型2 变量名2){//处理异常的方式2}catch(异常类类型3 变量名3){//处理异常的方式3}...finally{//一定会执行的代码}
    

说明:

  •  1.finally是可选的
    
  •  2.使用try将可能出现异常的代码包装一起来,执行过程中一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,就会去catch中匹配,一旦try中的异常对象匹配到,就开始处理异常,处理结束后,跳出当前的try-catch结构继续执行其后的代码
    
  •  3.catch中异常没有子父类关系,则声明在上下都无所谓但catch中异常有子父类关系,则子类声明在上,父类声明在下,否则出错。
    
  •  4.常用的异常对象处理方式:①String getMessage() ②printStackTrace()
    
  •  5.在try中声明的变量,在try外面不能用
    

体会:

  •  体会1:使用try-catch-finally处理编译时异常,是得程序编译时就不再报错,但运行时仍报错,相当于使用try-catch-finally将一个编译时可能出现的异常,延迟到运行时出现体会2:开发中由于运行时异常比较常见,所以我们通常就不针对运行时异常编写try-catch-finally了。针对编译时异常,我们说一定要考虑异常的处理
    
package com.atguigu.java;import org.junit.Test;public class ExceptionTest1 {@Testpublic void test1() {String str = "123";str = "abc";try {int num = Integer.parseInt(str);//一旦抛出对象后,其后代码就不再执行了(try里的)System.out.println("hello-1");} catch (NumberFormatException e) {// TODO: handle exception//System.out.println("出现数值转化异常");
//          System.out.println(e.getMessage());//e.printStackTrace();}catch (NullPointerException e) {// TODO: handle exceptionSystem.out.println("出现数值转化异常");}
//      System.out.println(num);System.out.println("hello-2");}
}

异常处理的方式二:throws + 异常类型

  •  1.“throws + 异常类型”写在方法的声明处,指明此方法执行时候,可能会抛出异常的类型,一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象满足throws后异常类型时就会被抛出,异常代码后的代码就不执行了
    
  •  2. 体会:try-catch-finally:真正的将异常处理掉,throws的方式只是将异常抛给了方法的调用者,并没有将异常处理掉
    
package com.atguigu.java;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;public class ExceptionTest2 {public static void main(String[] args) {try {method2();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//      method3();}public static void method3() {try {method2();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void method2() throws FileNotFoundException, IOException {method1();}public static void method1() throws FileNotFoundException,IOException {File file = new File("hello.txt");FileInputStream fis = new FileInputStream(file);int data = fis.read();while(data != -1){System.out.print((char)data);data = fis.read();}fis.close();}}

try-catch-finally中finally使用

  •   1.finally可选
    
  •  2.finally声明的是一定会被执行的代码,及时catch中有出现异常了,或try中有return语句,或catch中return语句
    
  •  3.像数据库连接、输入输出流、网络编程中的Socket等资源,JVM不能自动回收,需要我们手动的资源释放,此时的资源释放就需要声明在finally中
    
package com.atguigu.java;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;import org.junit.Test;public class FinallyTest {@Testpublic void test2(){FileInputStream fis = null;try {File file = new File("hello1.txt");fis = new FileInputStream(file);int data = fis.read();while(data != -1){System.out.print((char)data);data = fis.read();}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {if(fis != null)fis.close();} catch (IOException e) {e.printStackTrace();}}}@Testpublic void testMethod() {int num = method();System.out.println(num);}public int method() {try {int[] arr = new int[10];System.out.println(arr[10]);return 1;} catch (ArrayIndexOutOfBoundsException e) {// TODO: handle exceptione.printStackTrace();return 2;}finally {System.out.println("我一定会被执行****");//return 3;}}@Testpublic void test1() {try {int a = 10;int b = 0;System.out.println(a / b);} catch (ArithmeticException e) {// TODO: handle exceptione.printStackTrace();int[] arr = new int[10];System.out.println(arr[10]);}catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally {System.out.println("一定会执行的代码");}}
}

java中的异常及其处理相关推荐

  1. 深入探讨Java中的异常与错误处理

    Java中的异常处理机制已经比较成熟,我们的Java程序到处充满了异常的可能,如果对这些异常不做预先的处理,那么将来程序崩溃就无从调试,很难找到异常所在的位置.本文将探讨一下Java中异常与错误的处理 ...

  2. Java中的异常和处理详解

    Java中的异常和处理详解 参考文章: (1)Java中的异常和处理详解 (2)https://www.cnblogs.com/lulipro/p/7504267.html 备忘一下.

  3. 完成这个例子,说出java中针对异常的处理机制。

    有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常,在ClassA类中有一个方法a,请在这个方法中调用b,然后抛出异常.在客户端有一个类为TestC,有一个方 ...

  4. JAVA中的异常的触发_java中的异常

    在日常的程序开发中难免会出现遗漏并且就算代码没有问题可是由于程序运行环境的内存不够了,磁盘满了,网络连接问题等这些非正常的情况在java中都称之为异常.在java中对异常的处理有统一的异常处理机制,今 ...

  5. Java中的异常 Exception

    Java中的异常 Exception java.lang.Exception类是Java中所有异常的直接或间接父类.即Exception类是所有异常的根类. 比如程序: public class Ex ...

  6. java中的异常种类和区别以及处理机制和区别

    java中的异常种类和区别以及处理机制和区别 按照异常需要处理的时机分为编译时异常(也叫强制性异常)也叫 CheckedException 和运行时异常(也叫非强制性异常)也叫 RuntimeExce ...

  7. Java 中的异常和处理详解

    2019独角兽企业重金招聘Python工程师标准>>> 简介 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常.异常发生时,是任程序自生自灭,立刻退 ...

  8. Java中测试异常的多种方式

    Java中测试异常的多种方式 参考文章: (1)Java中测试异常的多种方式 (2)https://www.cnblogs.com/huang0925/p/3663074.html 备忘一下.

  9. JAVA中常见异常小结

    JAVA中常见异常小结 参考文章: (1)JAVA中常见异常小结 (2)https://www.cnblogs.com/lq147760524/p/6926175.html (3)https://ww ...

  10. java exception 包_什么是Java中的异常包装?

    Java中的异常包装是什么? 在异常处理中有什么用? 它与异常传播有何不同? Exception wrapping is when you catch an exception, wrap it in ...

最新文章

  1. 整个html和内部html,什么是HTML?
  2. c语言:宏里面参数不加括号容易出错,在使用时尽量加括号及举例
  3. C语言链表的转置算法,c语言编程集 数据结构 顺序表 点链表 数制转换 矩阵转置.doc...
  4. 易思汇完成近亿元B轮融资,信中利投资
  5. leetcode1046. 最后一块石头的重量(堆)
  6. oracle-11g-R2监听文件配置
  7. Java字符串找出4个字节长度的字符
  8. jquery-ui寺
  9. 字符集及其存储方式(解决乱码问题)
  10. 最新最全 快递公司编码 更新时间2020.07.31
  11. 2022版首发,阿里Java开发手册(黄山版).PDF
  12. php 下载函数太慢,php的fread函数的一个巨大的坑
  13. ftp打开方式更改为资源管理器方法
  14. 学习hutool源码TreeUtil.build()得到了什么
  15. 从网瘾少年逆袭拿到微软、字节等offer(上)
  16. java手机 上网_Java也懂智能! 中低端手机上网小攻略
  17. 知识蒸馏系列(一):三类基础蒸馏算法
  18. 如何轻松停用WordPress插件(入门指南)
  19. 【案例教程】Python气象海洋数据可视化到常见数据分析方法(折线图、柱状图、errorbar图、流场矢量、散点图、风玫瑰图、流场矢量、填色及等值线+地图)
  20. Chrome 插件:无痕浏览模式下加载插件、启用插件设置方法

热门文章

  1. 常见数字IC设计、FPGA工程师面试题
  2. Replication Controller、Replica Set
  3. Hadoop(十)Hadoop IO之数据完整性
  4. Windows自带的端口转发工具netsh使用方法_DOS/BAT
  5. vue组件中的样式属性:scoped,解决在父组件中无法修改子组件样式问题
  6. 《C++ primer》--第三章
  7. canvas绘制弯月
  8. VC 下 64bit 整数的显示和读取格式化字串
  9. 怎样修改flash builder注释里的@author
  10. Metro中文件夹和文件的创建