Exceptions
2015-10-14 16:23
459 查看
Exceptions
When a program violates the semantic constraints of the Java programming language, the Java virtual machine signals this error to the program as an exception. An example of such a violation is an attempt to index outside the bounds of an array. Some programming languages and their implementations react to such errors by peremptorily terminating the program; other programming languages allow an implementation to react in an arbitrary or unpredictable way. Neither of these approaches is compatible with the design goals of the Java platform: to provide portability and robustness. Instead, the Java programming language specifies that an exception will be thrown when semantic constraints are violated and will cause a non-local transfer of control from the point where the exception occurred to a point that can be specified by the programmer. An exception is said to be thrown from the point where it occurred and is said to be caught at the point to which control is transferred.Programs can also throw exceptions explicitly, using
throwstatements (§14.18).
Explicit use of
throwstatements provides an alternative to the old-fashioned style of handling error conditions by returning funny values, such as the integer value
-1where a negative value would not normally be expected. Experience shows that too often such funny values are ignored or not checked for by callers, leading to programs that are not robust, exhibit undesirable behavior, or both.
Every exception is represented by an instance of the class
Throwableor one of its subclasses; such an object can be used to carry information from the point at which an exception occurs to the handler that catches it. Handlers are established by
catchclauses of
trystatements (§14.20). During the process of throwing an exception, the Java virtual machine abruptly completes, one by one, any expressions, statements, method and constructor invocations, initializers, and field initialization expressions that have begun but not completed execution in the current thread. This process continues until a handler is found that indicates that it handles that particular exception by naming the class of the exception or a superclass of the class of the exception. If no such handler is found, then the method
uncaught-Exceptionis invoked for the
ThreadGroupthat is the parent of the current thread-thus every effort is made to avoid letting an exception go unhandled.
The exception mechanism of the Java platform is integrated with its synchronization model (§17), so that locks are released as
synchronizedstatements (§14.19) and invocations of
synchronizedmethods (§8.4.3.6, §15.12) complete abruptly.
This chapter describes the different causes of exceptions (§11.1). It details how exceptions are checked at compile time (§11.2) and processed at run time (§11.3). A detailed example (§11.4) is then followed by an explanation of the exception hierarchy (§11.5).
相关文章推荐
- centos6.5 搭建postgresql小结
- OC与Swift混编
- wx.Timer来设置定时事件(绑定定时器)
- Objective-C高级编程:iOS与OS X多线程和内存管理读书笔记
- 【官方搭建入门】JEEWX 捷微开发环境搭建必读
- Win7系统怎样彻底删除收藏夹里的网站链接?Win7系统快速删除收藏夹网站链接的方法
- 【扣丁学堂】程序员如何提高工作效率?
- multimap案例
- Storm(二)Storm基本术语
- PHP将HTML转换成各种格式图片或PDF
- C++Primer第五版 练习11.38-1单词统计程序(解答)
- 文本编码的智能识别(它的两个指导哲学真是绝妙)
- servlet3.0 头部标准
- 重复造轮之权限管理系统
- css制作简单下拉菜单
- PHP变量
- UIView中的坐标转换
- android 中解析RSSxml文件 GMT格式时间格式化
- Apache目录访问权限配置详解
- springMVC 请求404异常或静态资源无法访问