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

(singke) #1
ptg7068951

262 HOUR 18:Handling Errors in a Program


Q&A
Q. Is it possible to create your own exceptions?
A. Yo u c a n c r e a t e y o u r o w n e x c e p t i o n s e a s i l y by m a k i n g t h e m a s u b c l a s s
of an existing exception,such as Exception, the superclass of all
exceptions. In a subclass of Exception, there are only two methods you
might want to override: Exception()with no arguments and
Exception()with a Stringas an argument. In the latter,the string
should be a message describing the error that has occurred.

Q. Why doesn’t this hour cover how to throw and catch errors in addition
to exceptions?
A. Java divides problems into ErrorsandExceptionsbecause they differ
in severity. Exceptions are less severe,so they are something that
should be dealt with in your programs using try-catchorthrowsin the
method declaration. Errors,on the other hand,are more serious and
can’t be dealt with adequately in a program.
Tw o e x a m p l e s o f t h e s e e r r o r s a r e s t a c k ov e r f l o w s a n d o u t - o f - m e m o r y
errors. These can cause the Java interpreter to crash,and there’s no
way you can fix them in your own program as the interpreter runs it.

Q. What is the oldest comic strip that’s still running in newspapers?
A. Katzenjammer Kids, which was created by Rudolph Dirks in 1897 and is
still offered today by King Features Syndicate. The strip was started
only two years after the first comic strip,The Yellow Kid, and is the first
to use speech balloons.
Dirks,a German immigrant to the United States,was inspired to create
the rebellious kids Hans and Fritz by a children’s story from his native
country. He quit the strip in 1912 in a contractual dispute and was suc-
ceeded by Harold Knerr,who wrote and drew it until 1949. There have
been five subsequent cartoonists working on it. Hy Eisman has been
doing it since 1986.
The word katzenjammerliterally means “the wailing of cats” in German,
but it’s more often used to describe a hangover.
Free download pdf