栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 基础--- 异常 Exception

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 基础--- 异常 Exception

Java 基础--- 异常 Exception
  • Exception 类型
  • 使用Exception
  • Throwable Class
  • Example
  • finally 关键字
  • Chained Exception
  • 自定义Exception

Exception 类型
  • 所有的异常都从 java.lang.Throwable 继承
  • Exception class可以被分为三种类型 system errors, exception, runtime exceptions
  • Error: There is little you can do beyond notifying the user and trying to terminate the program gracefully. Example: LinkageError, VirtualMachineError
  • Exceptions: describes errors caused by your program and by external circumstances. Example: ClassNotFoundException, IOException,
  • Runtime Exceptions: describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Example: NullPointerException, IndexOutOfBoundsException
  • Unchecked Exceptions: including runtime exceptions, error and their subclasses
    In most cases, unchecked exceptions reflect programming logic errors that are unrecoverable. For example, NullPointerException
  • Checked Exceptions: other exceptions are checked Exceptions.
使用Exception

声明 Exception

public void myMethod() throws Exception1
public void myMethod() throws Exception1, Exception2, ..., ExceptionN

抛出Exception

 throw new IllegalArgumentException("Wrong Argument");
IllegalArgumentException ex = new IllegalArgumentException("Wrong Argument");
throw ex;
  • 注意throw是抛出异常的时候用, throws是声明异常的时候用

Catch Exception

  • Note: 在method2中,如果exception不是ex3,没有被catch住,则程序直接跳会method1,statment5和statment6都不会执行。
Throwable Class



使用 Throwable

Example


finally 关键字
  • 不管有没有exception发生, finally总会被执行
  • 如果try中出现exception则程序不会回到try中, 执行完finally之后会继续往下执行
  • 即使在finally之前有return, finally里的代码还是会被执行

执行顺序 try - catch -finally

(假设try里有return)

  • 如果没有exception发生: statement4会被执行, statement 5不会被执行
  • 如果有exception 并且是 Exception 1, 则4和5都会被执行
  • 如果有exception 并且不是 Exception 1, 则4会被执行, 然后程序报错

(假设try里没有return)

  • 如果没有exception发生: statement4, statement 5都会被执行
  • 如果有exception 并且是 Exception 1, 则4和5都会被执行
  • 如果有exception 并且不是 Exception 1, 则4会被执行, 然后程序报错
  • 没有exception: try和finally里都有return: 会使用finally里的return
  • 有exception, try,catch, finally里都有return: 会执行finally里的return

总结:

  • 如果finally里有return则一定会执行finally里的return
  • 如果finally里没有return
  • 没有exception的情况会执行try里的return, 如果try里没有return会继续finally之后的代码执行
  • 有exception的情况会执行catch里的return,如果catch里没有return会继续finally之后的代码执行

在finally中修改变量

  • finally相当于一个方法, 变量相当于参数, 所以遵循java值传递的规则
  • 下图中finally没有影响tryli的变量
  • 以下代码中finally中的put方法起作用了, 但是map = null没有起作用
Chained Exception
  • Sometimes, you may need to throw a new exception (with additional information) along with the original exception. This is called chained exceptions.
自定义Exception

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号