SQL/MX Programming Manual for Java

SQLJ Programming
HP NonStop SQL/MX Programming Manual for Java523726-003
3-62
Java Exceptions
Java Exceptions
If you code a catch block to handle SQL exceptions and the try block includes Java
methods that throw other Java exceptions, you should code one or more catch blocks
to handle those exceptions. For example, you might handle all Java exceptions by
catching java.lang.Exception, or you might handle specific Java exceptions by
catching subclasses of java.lang.Exception.
This code handles both SQL and Java exceptions that occur in the try block, and
prints one type of error message if an SQL exception is thrown and another type of
error message if a Java exception is thrown:
...
public static void main (String [] args) {
try {
showPrices();
MultiThread1 mt1 = new MultiThread1(new BigDecimal(186),
(double)15);
MultiThread1 mt2 = new MultiThread1(new BigDecimal(186),
(double)2);
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
t1.start(); // Throws a Java exception called
// java.lang.IllegalThreadStateException
t2.start();
t1.join(); // Throws a Java exception called
// java.lang.InterruptedException
t2.join();
showPrices(); // Throws an SQL exception
}
catch (SQLException se) {
System.err.println("SQL exception: " + se);
}
catch (Exception e) {
System.err.println("Exception: " + e);
}
}
...
The first catch block handles SQL exceptions that occur during the invocation of the
showPrices() method, which performs an SQL operation. The second catch block
handles Java exceptions that occur during the invocation of the start() and join()
methods.