java_3_exceptions.pdf
(
951 KB
)
Pobierz
„Exceptions”
Exceptions
Handling errors with exceptions
Golden rule of programming:
Errors occur in software programs.
What really matters is what happens
after
the error occurs:
• How is the error handled?
• Who handles it?
• Can the program recover, or should it just die?
Exceptions
Exceptions - fundamentals
• An
exception
is an event that occurs during the execution of a program that
disrupts the normal flow of instructions.
•
Many kinds of errors can cause exceptions--problems
ranging from
serious hardware errors, such as a hard disk crash, to simple programming
errors, such as trying to access an out-of-bounds array element.
• When such an error occurs within a Java method, the method creates an
exception object and hands it off to the runtime system.
• The exception object contains information about the exception, including its
type and the state of the program when the error occurred. The runtime
system is responsible for finding some code to handle the error. In Java
terminology, creating an exception object and handing it to the runtime
system is called
throwing an exception.
Exceptions
Exceptions – fund...(2)
• After a method throws an exception, the runtime system leaps into action to
find someone to handle the exception. The set of possible "someones" to
handle the exception is the set of methods in the call stack of the method
where the error occurred. The runtime system searches backwards through
the call stack, beginning with the method in which the error occurred, until it
finds a method that contains an appropriate
exception handler.
• An exception handler is considered appropriate if the type of the exception
thrown is the same as the type of exception handled by the handler. The
exception handler chosen is said to
catch the exception.
• If the runtime system exhaustively searches all of the methods on the call
stack without finding an appropriate exception handler, the runtime system
(and consequently the Java program) terminates.
Exceptions
Exceptions advantages
1. Separating error handling code from „regular” code.
Suppose that you have a function that reads an entire file into memory. In
pseudo-code, your function might look something like this:
readFile { open the file; determine its size; allocate that much memory; read the
file into memory; close the file; }
At first glance this function seems simple enough, but it ignores all of these
potential errors:
• What happens if the file can't be opened?
• What happens if the length of the file can't be determined?
• What happens if enough memory can't be allocated?
• What happens if the read fails?
• What happens if the file can't be closed?
Plik z chomika:
Adamus666GT
Inne pliki z tego folderu:
java_1_basics.pdf
(1509 KB)
java_2_object_classes_interfaces_packages_annotations.pdf
(2388 KB)
java_3_exceptions.pdf
(951 KB)
java_1_basics.ppt
(641 KB)
java_2_object_classes_interfaces_packages_annotations.ppt
(857 KB)
Inne foldery tego chomika:
Bazy Danych
CISCO - sieci
Systemy operacyjne 2
Systemy wbudowane
Zgłoś jeśli
naruszono regulamin