Exceptions in JDK

Exceptions

  1. Exception Overview:

    • An exception in Java is a signal to the Java Virtual Machine (JVM) that a program has violated the semantic constraints of the Java programming language.
    • Exceptions provide a means for error handling and are used to transfer control from the point where an error occurred to a point that can handle it.
  2. Exception Terminology:

    • An exception is said to be “thrown” from where it occurred and “caught” at the point to which control is transferred.
    • Programs can explicitly throw exceptions using throw statements.
  3. Exception Classes:

    • All exceptions are represented by instances of the class Throwable or one of its subclasses.
    • Exception and Error are direct subclasses of Throwable.
      • Exception: Superclass of all exceptions from which a program may wish to recover.
      • Error: Superclass of exceptions from which a program is not expected to recover.
    • RuntimeException: Direct subclass of Exception, superclass of all exceptions that may be thrown during expression evaluation but from which recovery may be possible.
  4. Checked vs. Unchecked Exceptions:

    • Checked exceptions are exceptions that must be either caught or declared in the method’s throws clause.
    • Unchecked exceptions are not checked at compile time and include RuntimeException and Error.
  5. Exception Handling:

    • Exception handling is done using try, catch, and finally blocks.
    • The JVM will search for a catch block that can handle the thrown exception starting from the nearest enclosing try block.
  6. Compile-Time Checking:

    • The Java compiler checks for the presence of handlers for checked exceptions that can be thrown by a method or constructor.
    • If a method or constructor can throw a checked exception and does not handle it, it must declare it with a throws clause.
  7. Asynchronous Exceptions:

    • Asynchronous exceptions are exceptions that can occur at any point in the execution of a program.
    • They can be caused by invoking the deprecated stop method of Thread or ThreadGroup, or by an internal error or resource limitation in the JVM.
  8. Run-Time Handling:

    • When an exception is thrown, control is transferred to the nearest dynamically enclosing catch clause that can handle the exception.
    • If no catch clause is found, the current thread is terminated, and uncaught exception handlers are invoked.
  9. Finally Block:

    • A finally block is used to ensure that a block of code is always executed after another block, even if the latter completes abruptly.
    • If a try or catch block completes abruptly, the finally block is executed during the propagation of the exception.
  10. Exception Propagation:

    • If an exception is not caught by any catch block, it propagates up the call stack until it is caught or the thread is terminated.

See
https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值