Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

WHAT YOU’LL LEARN IN
THIS HOUR:
.How to respond to excep-
tions in your Java programs
.How to create methods
that ignore an exception,
leaving it for another class
to handle
.How to use methods that
cause exceptions
.How to create your own
exceptions

Errors, the bugs, blunders, and typos that prevent a program from running
correctly, are a natural part of the software development process.
“Natural” is probably the kindest word that’s ever been used to describe
them. In my own programming, when I can’t find the cause of an elusive
error, I use words that would make a gangsta rapper blush.


Some errors are flagged by the compiler and prevent you from creating a
class. Others are noted by the interpreter in response to a problem that
keeps it from running successfully. Java divided errors into two categories:


. Exceptions—Events that signal an unusual circumstance has taken
place as a program runs
. Errors—Events that signal the interpreter is having problems that
might be unrelated to your program


Errors normally aren’t something a Java program can recover from, so
they’re not the focus of this hour.You might have encountered an
OutOfMemoryErroras you worked on Java programs; nothing can be done
to handle that kind of error. The program exits with the error.


Exceptions often can be dealt with in a way that keeps a program running
properly.


Exceptions


Althoughyou are just learning about them now, you have probably
become well-acquainted with exceptions during the last 17 previous hours.
These errors turn up when you write a Java program that compiles suc-
cessfully but encounters a problem as it runs.


HOUR 18


Handling Errors in a Program

Free download pdf