- 浏览: 1134514 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (411)
- Java Foundation (41)
- AI/机器学习/数据挖掘/模式识别/自然语言处理/信息检索 (2)
- 云计算/NoSQL/数据分析 (11)
- Linux (13)
- Open Source (12)
- J2EE (52)
- Data Structures (4)
- other (10)
- Dev Error (41)
- Ajax/JS/JSP/HTML5 (47)
- Oracle (68)
- FLEX (19)
- Tools (19)
- 设计模式 (4)
- Database (12)
- SQL Server (9)
- 例子程序 (4)
- mysql (2)
- Web Services (4)
- 面试 (8)
- 嵌入式/移动开发 (18)
- 软件工程/UML (15)
- C/C++ (7)
- 架构Architecture/分布式Distributed (1)
最新评论
-
a535114641:
LZ你好, 用了这个方法后子页面里的JS方法就全不能用了呀
页面局部刷新的两种方式:form+iframe 和 ajax -
di1984HIT:
学习了,真不错,做个记号啊
Machine Learning -
赵师傅临死前:
我一台老机器,myeclipse9 + FB3.5 可以正常使 ...
myeclipse 10 安装 flash builder 4.6 -
Wu_Jiang:
触发时间在将来的某个时间 但是第一次触发的时间超出了失效时间, ...
Based on configured schedule, the given trigger will never fire. -
cylove007:
找了好久,顶你
Editable Select 可编辑select
http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Throwable Class and Its Subclasses:
Java中方法的调用构成方法调用栈,方法调用栈中方法的顺序与方法的被调用顺序是相反的。
Java中程序的任何一处抛出Throwable对象后,都是采用按方法调用栈逐级上溯(即与方法调用顺序相反的顺序逐级上溯)的机制来搜寻可以处理被抛出的Throwable对象的方法(注意不管是Checked Exceptions 还是 Unckecked Exceptions,都是使用这种机制处理的)。如果找到了,则异常对象在该方法中被处理;如果搜寻所有方法直至程序入口都未能找到一个可以处理该Throwable对象的方法,则程序退出。如下面两个图所示:
The call stack.
Searching the call stack for the exception handler.
Checked Exceptions & Unckecked Exceptions:
Checked Exceptions:
any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
checked means they will be checked at compiletime it self,so Checked exceptions must be explicitly caught or propagated(只有两条路:使用try-catch-finally捕获它,或者在方法声明中throws它)。
Unckecked Exceptions:
RuntimeException, Error, and their subclasses
unchecked means you need not handle the unchecked exceptions and they will be handled by the JVM.
什么时候使用checked exception,什么时候使用unchecked exception?
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
对checked exception,什么时候应该抛出,什么时候应该捕获?Catch or Propagate Exceptions?
You might be wondering whether you should catch or propate exceptions thrown in your program. It depends on the situation. In many applications you can't really do much about the exception but tell the user that the requested action failed. In these applications you can usually catch all or most exceptions centrally in one of the first methods in the call stack. You may still have to deal with the exception while propagating it though (using finally clauses). For instance, if an error occurs in the database connection in a web application, you may still have to close the database connection in a finally clause, even if you can't do anything else than tell the user that the action failed. How you end up handling exceptions also depends on whether you choose checked or unchecked exceptions for your application. There is more on that in other texts in the error handling trail.
throws和throw的区别
引用
throw用来在语句中抛出一个异常,而throws用在方法声明中,表示这个方法会抛出某类异常。
throw语法:throw <异常对象>
在方法声明中,添加throws子句表示该方法将抛出异常。
throws语法:[<修饰符>]<返回值类型><方法名>([<参数列表>])[throws<异常类>]
其中:异常类可以声明多个,用逗号分割。
区别:
throws可以单独使用;
而对throw:
如果语句中throw抛出的是checked exception,则该checked exception必须被捕获(try-catch[-finally])或者抛出(方法声明中throws);所以可以认为,使用throw抛出checked exception的时候throw不可以单独使用,必须和try-catch[-finally] 或 throws配合使用;
如果语句中throw抛出的是unchecked exception(RuntimeException,Error,及他们的子类),unchecked exception是不需要你在代码中做处理的;可以认为,使用throw抛出unchecked exception的时候throw可以单独使用。
在方法的一个分支中(主干分支或任意一个条件分支,条件分支如if分支、else分支),throw一个Throwable对象,则如果该分支中throw子句后还存在代码块,则这些代码块会被认为是Unreachable,无法编译通过。throw语法:throw <异常对象>
在方法声明中,添加throws子句表示该方法将抛出异常。
throws语法:[<修饰符>]<返回值类型><方法名>([<参数列表>])[throws<异常类>]
其中:异常类可以声明多个,用逗号分割。
区别:
throws可以单独使用;
而对throw:
如果语句中throw抛出的是checked exception,则该checked exception必须被捕获(try-catch[-finally])或者抛出(方法声明中throws);所以可以认为,使用throw抛出checked exception的时候throw不可以单独使用,必须和try-catch[-finally] 或 throws配合使用;
如果语句中throw抛出的是unchecked exception(RuntimeException,Error,及他们的子类),unchecked exception是不需要你在代码中做处理的;可以认为,使用throw抛出unchecked exception的时候throw可以单独使用。
//存在三个分支:main主分支、if分支、else分支 public static void main( String[] args ) throws Throwable { if(true) { throw new Throwable("err"); //System.out.println("ddd"); //不可达 } else { System.out.println(""); //可达 } throw new NullPointerException("null err"); //可达 //m1(); //不可达 }
Sources:
Checked or Unchecked Exceptions?
http://tutorials.jenkov.com/java-exception-handling/checked-or-unchecked-exceptions.html
Basic try-catch-finally Exception Handling in Java
http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html
发表评论
-
J2SE Evolution
2013-04-11 15:39 1169Java 7 New Features Java SE 7 ... -
未完 Java: IO & NIO(new I/O)
2013-01-11 20:56 2004适用: event and data-driven apps ... -
未完 java设计: naming convention | 命名规范
2012-11-20 16:45 2106应该遵循的规范: 类/接口/属性名,使用名词或形容词 ... -
未完 Java: enum 枚举
2012-11-19 20:29 1779http://stackoverflow.com/que ... -
Java多线程之 concurrent 并发包
2012-11-01 07:47 1978Java Tutorials -> Concur ... -
未完 Java Tips & Tricks & Notes
2012-09-12 10:00 1090Hidden Features of Java: h ... -
未完 Java Socket
2012-09-12 08:42 991Java SocketJava SocketJava Sock ... -
Java For-each Loop & Iterable | 增强型For循环和Iterable接口
2012-09-11 21:50 2023增强型For循环没什么好说的,Just see link ... -
未完 Java Collections | 容器
2012-09-06 11:35 1806Sources: http://docs.oracle.com ... -
Java object Initialization (class Instantiation) | 对象的初始化(即类的实例化)
2012-09-03 09:12 2963类实例即对象 ... -
未完Java class&interfac 's Loading, Linking and Initializing | 类与接口的加载、链接和初始化
2012-08-31 19:01 1633JVM装载一个类的时候,首先检查他有没有父类,如果有父类则装载 ... -
未完 java Static 总结
2012-08-31 18:47 1376static可以用来修饰: 字段 Fields 方法 Meth ... -
未完 JVM Runtime Data Areas & Java Memory Model | 内存分配模型 & Java数据存储
2012-08-31 18:43 1859Java虚拟机内存分配模型 需精读:Chapter 5 of ... -
Java Data Types & Literals | 数据类型 和 字面量
2012-08-30 18:12 3874Java数据类型划分: OR http:// ... -
未完 Variables 变量 (Instance/Class/Local)
2012-08-29 10:59 1656Local/Instance/Class Variables ... -
未完 Regular Expressions | 正则表达式
2011-08-25 11:43 1497Extended Regular Expression ... -
java Date(util.Date/sql.Date/sql.Timestamp/sql.Time) & Oracle DATE Type 时分秒 精度问题
2011-05-17 09:32 3903遇到的问题描述: 数据库为Oracle,其jdbc驱动为ojd ... -
Java byte code (bytecode)
2011-05-04 02:55 3856keys: bytecode, byte code, opco ... -
Java Classloading Mechanism : ClassLoader & ASM & 动态字节码增强
2011-04-21 13:29 2390Setting the class path: http:// ... -
class literal & instance.getClass() & Class.forName(String className)
2011-04-20 12:33 2304常用的几种取得Class类实例的方式: 1 class lit ...
相关推荐
Exception JAVA 自定义异常 Exception JAVA 自定义异常 Exception
exception基础,异常的基本用法。
java 除0发生的算术异常(ArithmeticException) java 除0发生的算术异常(ArithmeticException)
Java异常处理的几个Example代码,让我们了解异常类型、声明异常、抛出和捕获异常、从异常获取信息、创建自定义异常等内容。
java 异常 问题收集 Exception
如果java提供的系统异常类型不能满足程序设计的需求,那么可以设计自己的异常类型。 从java异常类的结构层次...另外一种问题是程序运行错误,java定义为Exception,这种情况下,可以通过程序设计调整来实现异常处理。
深入理解java异常(异常中的Error+异常中的Exception+检查异常+运行时异常+异常处理+throws关键字等)
关于javaexception的讲解,很透彻,适合中高级别的!
在 Java 中,Object 类的子类中就包含有异常类 Throwable 类,该类中包含直接子类 Exception 类和 Error 类。其中 Error 错误类对象是由 Java 虚拟机生成,并且将错误抛出。在程序中一般不会处理此类错误。 Java 的...
Java自定义异常处理详细介绍,包括了很多比较经典的案例,可以自己研究自定义异常
Java异常架构与异常关键字 Java异常简介 Java异常架构 1. Throwable 2. Error(错误) 3. Exception(异常) 运行时异常 编译时异常 4. 受检异常与非受检异常 受检异常 非受检异常 Java异常关键字 Java异常处理 声明...
Java中的异常分为三种类型:检查型异常(Checked Exception)、非检查型异常(Unchecked Exception)和错误(Error)。 检查型异常(Checked Exception): 这些异常在代码中必须进行显式处理,否则编译器会报错。 ...
集结JAVA编程中遇到的各种异常解析, 集结JAVA编程中遇到的各种异常解析,
java异常练习题,北大青鸟学士后课后练习,主要是异常处理的小测验
在初学java中一般都会出现各种异常!比如: NO.3 java.lang.ArithmeticException java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.ArithmeticException ...
下面小编就为大家带来一篇Java异常 Exception类及其子类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在Java中,异常处理是一种用于处理在程序执行期间可能出现的错误或异常条件的机制。这种机制允许程序员定义在特定错误或异常情况下应该如何执行代码。Java的异常处理机制主要包括try-catch-finally语句和throw语句。...
Checked Exception(受检的异常)2---马克-to-win java视频
java异常.pdf,里面包括了java的各种异常,而且有相应的代码示例!
主要介绍了Java 常见异常(Runtime Exception )详细介绍并相关资料,大家在开发Java 应用软件的时候经常会遇到各种异常这里帮大家整理了一部分,并解释如何解决,需要的朋友可以参考下