Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə13/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   9   10   11   12   13   14   15   16   ...   47
Java Interview Guide

Unchecked Exceptions
Unchecked exceptions represent a defect in the application, such as invoking
a method on a null object reference or casting an object to an invalid type.
A method can throw an unchecked exception without forcing its callers
to establish an error-handling policy. Unchecked exceptions extend the
RuntimeException
class. Common unchecked exceptions include
NullPointerExceptions
, IllegalArgumentExceptions,
and ClassCastExceptions.


Checked Exceptions
Checked exceptions represent a defect that occurs outside of the control of the
application, such as opening a file that doesn’t exist or connecting to a database
with invalid credentials. A method that throws a checked exception forces its
callers to establish an error-handling policy. Callers must either surround the
method in a try/catch block or add the exception to its method declaration
to push the responsibility further up the call stack. Checked exceptions extend
the Exception class. Common checked exceptions include IOExceptions,
FileNotFoundExceptions
, and InterruptedExceptions.


Unchecked Exceptions vs Checked Exceptions
Checked exceptions have been a source of contention in the Java community
because they are frequently overused. The official documentation states: “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.” When in doubt, unchecked
exceptions are preferred because it allows clients to choose whether or not
an error-handling policy is appropriate.


Errors
An Error is thrown by the JVM to indicate that a fatal condition has occurred.
Errors extend the Throwable class directly, which gives them the behavior
of unchecked exceptions. Common errors include OutOfMemoryErrors
or StackOverflowErrors.



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   9   10   11   12   13   14   15   16   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin