zoukankan      html  css  js  c++  java
  • Exceptions

    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 throw statements (§14.18).

    Explicit use of throw statements provides an alternative to the old-fashioned style of handling error conditions by returning funny values, such as the integer value-1 where 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 Throwable or 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 catch clauses of try statements (§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 ThreadGroup that 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 synchronized statements (§14.19) and invocations of synchronized methods (§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).

  • 相关阅读:
    从C,C++,JAVA和C#来看String库的发展(二)---JAVA和C#篇
    从C,C++,JAVA和C#看String库的发展(一)----C语言和C++篇
    C++ 对象的内存布局
    vlc sdl2.0 播放示例
    wafer2-nodejs 本地部署服务器
    python socket编程腾讯云下报错[Errno 99] Cannot assign requested address的解决方式
    小程序setData()使用和注意事项
    wafer2的几个简单示例
    Node.js中的模块接口module.exports
    Koa 中间件的执行顺序
  • 原文地址:https://www.cnblogs.com/hephec/p/4877823.html
Copyright © 2011-2022 走看看