O'Reilly logo

Java Programming with Oracle JDBC by Donald Bales

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling Exceptions

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.

Java Exception Handling

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.

Try blocks

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 try 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 SQLException.

A try statement can have any number of catch clauses necessary to handle ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required