If you’re a PL/SQL programmer, then the concept of exceptions will not be all that new to you. If you’re new to Java and have not previously used a programming language that uses exception handling, then this material may get confusing. Hang in there! By the time we’re done, you should have a fairly good idea of what exceptions are and how to deal with them in your JDBC programs.
In Java, exception handling allows you to handle exceptional conditions such as program-defined errors in a controlled fashion. When an exception condition occurs, an exception is thrown. The term thrown means that current program execution stops, and control is redirected to the nearest applicable catch clause. If no applicable catch clause exists, then the program’s execution ends.
Both the JVM and you -- explicitly in
your own code -- can throw an exception. Java uses a
try-catch-finally control block similar to
PL/SQL’s BEGIN-EXCEPTION-END block. The
statement encloses a block of code
that is “risky” -- in other words, which can throw an
exception -- and that you wish to handle in such a way as to
maintain control of the program in the event that an exception is
thrown. Exceptions thrown in a
try block are
handled by a
catch clause coded to catch an
exception of its type or one of its ancestors. For example, when
using JDBC, the exception type thrown is usually a
try statement can have any number of
catch clauses necessary to handle ...