Java中的异常Exception
涉及到异常类相关的文章:
(1)异常类不能是泛型的 http://www.cnblogs.com/extjs4/p/8888085.html
(2)Finally block may not complete normally的问题,参考文章:https://www.cnblogs.com/extjs4/p/9375400.html
(3)JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2
(4)关于动态代理的异常 http://www.importnew.com/28664.html
try{ }catch(Exception e){ }
上面的例子不会报错,虽然try块中不会抛出异常,所以catch中Exception或者Exception的父类时不要求try块中一定要抛出相应异常。
Throwable为顶层父类,派生出Error类和Exception类,如下图:

错误:Error类以及他的子类的实例,代表了JVM本身的错误。错误不能被程序员通过代码处理,Error很少出现。因此,程序员应该关注Exception为父类的分支下的各种异常类。
关于断言的java.lang.AssertionError也是无法处理的,如果出现错误,程序将终止。开启断言可在Eclipse中的VM arguments中传入参数:-ea 进行开启
异常:Exception以及他的子类,代表程序运行时发送的各种不期望发生的事件。可以被Java异常处理机制使用,是异常处理的核心。而且如果这些异常没有被处理,会导致线程退出。异常又分为非检查异常(unckecked exception)与检查异常(checked exception)。
由上图可以看出,继承
例1:
class SubThread extends Thread {
@Override
public void run() throws ArithmeticException{
int i = 0;
int x = i / i; // 抛出java.lang.ArithmeticException
System.out.println("SubThread end...");
}
}
运行时异常如果不处理最终还是会导致线程终止。
例2:
class SubThread extends Thread {
@Override
public void run() throws ArithmeticException{
int i = 0;
try {
int x = i / i;
} catch (Exception e) {
throw e;
}
System.out.println("SubThread end...");
}
}
抛出异常时没有具体进行处理,同样会导致线程终止。如果注释掉throw e语句后,则线程就可正常结束打印出SubThread end...
11.2.1. Exception Analysis of Expressions
A class instance creation expression (§15.9) can throw an exception class E iff either:
The expression is a qualified class instance creation expression and the qualifying expression can throw E; or
Some expression of the argument list can throw E; or
E is determined to be an exception class of the
throwsclause of the constructor that is invoked (§15.12.2.6); orThe class instance creation expression includes a ClassBody, and some instance initializer block or instance variable initializer expression in the ClassBody can throw E.
A method invocation expression (§15.12) can throw an exception class E iff either:
The method to be invoked is of the form Primary.Identifierand the Primary expression can throw E; or
Some expression of the argument list can throw E; or
E is determined to be an exception class of the
throwsclause of the method that is invoked (§15.12.2.6).
For every other kind of expression, the expression can throw an exception class E iff one of its immediate subexpressions can throw E.
eg1:
class FirstException extends Exception { }
class AA{
public AA() throws FirstException{}
}
// 方法上必须有FirstException,否则下面抛出FirstException都将报错
public void dd() throws FirstException {
AA a = new AA(){}; // // throw FirstException
Object b = new Object(){
// instance variable initializer
AA a = new AA(); // throw FirstException
// instance initializer block
{
AA a = new AA(); // // throw FirstException
}
};
}
11.2.2. Exception Analysis of Statements
A throw statement (§14.18) whose thrown expression has static type E and is not a final or effectively final exception parameter can throw E or any exception class that the thrown expression can throw.
For example, the statement throw new java.io.FileNotFoundException(); can throw java.io.FileNotFoundException only. Formally, it is not the case that it "can throw" a subclass or superclass of java.io.FileNotFoundException.
A throw statement whose thrown expression is a final or effectively final exception parameter of a catch clause C can throw an exception class E iff:
E is an exception class that the
tryblock of thetrystatement which declares C can throw; andE is assignment compatible with any of C's catchable exception classes; and
E is not assignment compatible with any of the catchable exception classes of the
catchclauses declared to the left of C in the sametrystatement.
A try statement (§14.20) can throw an exception class E iff either:
The
tryblock can throw E, or an expression used to initialize a resource (in atry-with-resources statement) can throw E, or the automatic invocation of theclose()method of a resource (in atry-with-resources statement) can throw E, and E is not assignment compatible with any catchable exception class of anycatchclause of thetrystatement, and either nofinallyblock is present or thefinallyblock can complete normally; orSome
catchblock of thetrystatement can throw E and either nofinallyblock is present or thefinallyblock can complete normally; orA
finallyblock is present and can throw E.
An explicit constructor invocation statement (§8.8.7.1) can throw an exception class E iff either:
Some expression of the constructor invocation's parameter list can throw E; or
E is determined to be an exception class of the
throwsclause of the constructor that is invoked (§15.12.2.6).
Any other statement S can throw an exception class E iff an expression or statement immediately contained in S can throw E.
关于异常的一些注意点:
(1)If a catch block handles more than one exception type, then the catch parameter is implicitly final.
(2)Rethrowing Exceptions with More Inclusive Type Checking(更具包容性的类型检查)
static class FirstException extends Exception { }
static class SecondException extends Exception { }
// Java7之前对异常的处理
public void rethrowExceptionPriorSE7(String exceptionName)
throws Exception {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
// Java7新增对异常的处理语法
public void rethrowExceptionSE7(String exceptionName)
throws FirstException, SecondException {
try {
if (exceptionName.equals("First")) {
throw new FirstException();
} else {
throw new SecondException();
}
} catch (Exception e) {
throw e;
}
}
In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:
- The
tryblock is able to throw it. - There are no other preceding
catchblocks that can handle it. - It is a subtype or supertype of one of the
catchclause's exception parameters.
如上的三条对Java7及后面的版本来说都需要进行检查,如下:
static class FirstException extends Exception { }
static class SecondException extends FirstException { }
// Java7新增对异常的处理语法
public void rethrowExceptionSE7(String exceptionName)
throws FirstException, SecondException {
try {
throw new FirstException();
} catch (SecondException e) {
throw e;
}
}
在try块中抛出父类异常,在catch中捕获子类异常,这是允许的。
参考:
(1)http://www.importnew.com/26613.html
(2)https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html
(3)https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html
Java中的异常Exception的更多相关文章
- java中的异常(Exception)
基本概念 将程序执行中发生的不正常的情况称为"异常".开发中的语法错误和逻辑错误不是异常 执行过程中的异常事件可分为两大类 错误(Error):Java虚拟机无法解决的严重问题.例 ...
- Java中的Checked Exception——美丽世界中潜藏的恶魔?
在使用Java编写应用的时候,我们常常需要通过第三方类库来帮助我们完成所需要的功能.有时候这些类库所提供的很多API都通过throws声明了它们所可能抛出的异常.但是在查看这些API的文档时,我们却没 ...
- 【Java心得总结二】浅谈Java中的异常
作为一个面向对象编程的程序员对于 下面的一句一定非常熟悉: try { // 代码块 } catch(Exception e) { // 异常处理 } finally { // 清理工作 } 就是面向 ...
- Java中的异常-Throwable-Error-Exception-RuntimeExcetpion-throw-throws-try catch
今天在做一个将String转换为Integer的功能时,发现Integer.parseInte()会抛出异常NumberFormatException. 函数Integer.parseInt(Stri ...
- Java中的异常详解
一.异常定义 阻止当前方法或作用域继续执行的问题,称为异常 二.异常分析 所有不正常类都继承Throwable类,这个类主要有两个子类Error类和Exception类.Error指系统错误 ...
- Java中的异常和处理详解
简介 程序运行时,发生的不被期望的事件,它阻止了程序按照程序员的预期正常执行,这就是异常.异常发生时,是任程序自生自灭,立刻退出终止,还是输出错误给用户?或者用C语言风格:用函数返回值作为执行状态?. ...
- Java中的异常简介
Java中异常的分类 Java中的异常机制是针对正常运行程序的一个必要补充,一般来说没有加入异常机制,程序也能正常运营,但是,由于入参.程序逻辑的严谨度,总会有期望之外的结果生成,因此加入异常机制的补 ...
- java中的异常类
Java中的异常: 1. Throwable是所有异常的根,java.lang.Throwable Throwable包含了错误(Error)和异常(Exception),Exception又包含了运 ...
- Java 中的异常
前段时间集合的整理真的是给我搞得心力交瘁啊,现在可以整理一些稍微简单一点的,搭配学习 ~ 突然想到一个问题,这些东西我之前就整理过,现在再次整理有什么区别嘛?我就自问自答一下,可能我再次整理会看到不一 ...
随机推荐
- BI使用者的角色
把企业中的BI使用者的角色分成如下几类: 系统管理员:没有数据权限:没有功能权限:负责配置其他人的权限:BI专家:拥有所有数据权限:拥有多维分析,报表查看,报表开发,模型开发权限:负责开发和维护BI系 ...
- Snapshot--使用Snapshot来还原数据库
在数据库升级时,为防止升级失败造成的影响,我们通常需要: 1.准备回滚脚本,用于失败后回滚 2.在升级前备份数据库,用于失败后恢复 但回滚脚本需要花费很长时间准备,而备份恢复会导致数据库长时间不可用, ...
- jsp页面都放在web-inf下面说是要防止用户直接访问jsp页面,为么不能直接访问jsp
为了保护那部分jsp页面,如果没有登录验证,那部分jsp用户可以直接访问,这样很不安全,放在WEB-INF下面,就使得只能WEB-INF文件夹外jsp页面调用里面的jsp,这样来使用,就比如我们有一个 ...
- java获取网络ip地址
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...
- winform在A窗体刷新B窗体,并改变窗体的属性
//A窗体设置B窗体的属性并刷新B窗体 Application.OpenForm["窗体名称"].Controls["控件名称"].visible=true;
- visualstudio部分快捷键
[工具快捷键] Ctrl+Shift+N: 新建项目 Ctrl+Shift+O: 打开项目 Ctrl+Shift+S: 全部保存 Shift+Alt+C: 新建类 Ctrl+Shift+A: 新建项 ...
- html Canvas 画图 能够选择并能移动
canvas 画图,能够选中所画的图片并且能够随意移动图片 <html xmlns="http://www.w3.org/1999/xhtml"> <head r ...
- 【总结】 BZOJ前100题总结
前言 最近发现自己trl,所以要多做题目但是Tham布置的题目一道都不会,只能来写BZOJ HA(蛤)OI 1041 复数可以分解成两个点,所以直接把\(R^2\)质因数分解一下就可以了,注意计算每一 ...
- 磁盘 blk_update_request: I/O error
1.尝试1: 解决 blk_update_request: I/O error, dev fd0, sector 0 错误 参考文档: https://bbs.archlinux.org/viewto ...
- 抓包工具Fiddler使用教程
一.基本原理 Fiddler 是以代理web服务器的形式工作的,它使用代理地址:127.0.0.1,端口:8888 二.Fiddler抓取https设置 1.启动Fiddler,打开菜单栏中的 Too ...