Subject Re: [Firebird-Java] Fb Exceptions in JayBird
Author James Calfee
Here it is again, the groups stripped my .java attachment. The example
program is below. You can read it just fine or use the backspace to
unwrap long lines before running it.

On Fri, 2011-01-14 at 11:25 -0500, James Calfee wrote:
>
> >Can you create a small java program that simulates both cases?
...
> > Roman
>

package testing;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class FbExceptionTest {

/**
* I would expect that in every exception thrown by the procedure
* P_TEST_EXCEPTION, the java exception would contain, somewhere, the
name
* of the original exception "TEST_EXCEPTION." This is true on only one
case
* (the first one).
*
* <br>
* Using FB 1.5 and JayBird 1.5 or 2.1.6 ... Runs and prints "Done"
*/
public static void main(String[] args) throws SQLException,
ClassNotFoundException {

Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection con = DriverManager.getConnection(
// any database will work
"jdbc:firebirdsql:localhost/3050:employee.fdb", "SYSDBA",
"masterkey");

con.setAutoCommit(false);
try {
Statement st = con.createStatement();
{
st.executeUpdate("CREATE EXCEPTION TEST_EXCEPTION '';");
st
.executeUpdate("CREATE PROCEDURE P_TEST_EXCEPTION ( MSG VARCHAR(128)) AS
BEGIN "
+ "IF(MSG IS NULL) THEN EXCEPTION TEST_EXCEPTION; ELSE EXCEPTION
TEST_EXCEPTION MSG; END");
try {
st.executeUpdate("{call P_TEST_EXCEPTION(NULL)}");
} catch (Exception e) {
assertEqual("TEST_EXCEPTION", e.getMessage().split("\n")[1]);

// Where is FBSQLException?
assertTrue(e.getCause() == null);
}
try {
st.executeUpdate("{call P_TEST_EXCEPTION('hiho')}");
} catch (SQLException e) {
assertFalse(e.getMessage().contains("TEST_EXCEPTION"));
assertEqual("hiho", e.getMessage().split("\n")[1]);
assertTrue(e.getCause() == null);
// I see FBSQLException in the debugger, however, the I
// could not find "TEST_EXCEPTION" string anywhere in
// the FBSQLException object.
}
con.rollback();
}

{
st.executeUpdate("CREATE EXCEPTION TEST_EXCEPTION 'default';");

st
.executeUpdate("CREATE PROCEDURE P_TEST_EXCEPTION ( MSG VARCHAR(128)) AS
BEGIN "
+ "IF(MSG IS NULL) THEN EXCEPTION TEST_EXCEPTION; ELSE EXCEPTION
TEST_EXCEPTION MSG; END");

try {
st.executeUpdate("{call P_TEST_EXCEPTION(NULL)}");
} catch (SQLException e) {
assertFalse(e.getMessage().contains("TEST_EXCEPTION"));

// Where is the word "default" ?
assertFalse(e.getMessage().contains("deafult"));

// there is no second line, just something like this:
// GDS Exception. 335544517. exception 22
assertEqual(1, e.getMessage().split("\n").length);
assertTrue(e.getCause() == null);
}
try {
st.executeUpdate("{call P_TEST_EXCEPTION('hiho')}");
} catch (Exception e) {
assertFalse(e.getMessage().contains("TEST_EXCEPTION"));
assertEqual("hiho", e.getMessage().split("\n")[1]);
assertTrue(e.getCause() == null);
}
con.rollback();
}

} finally {
con.close();
}
System.out.println("Done");
}

public static void assertEqual(String str1, String str2) {
if ((str1 == null && str2 != null) || str2 == null
|| !str1.equals(str2))

throw new Error("Values are not equal. Expecting '" + str1
+ "', but '" + str2 + "' was provided.");
}

public static void assertEqual(int int1, int int2) {
if (int1 != int2)

throw new Error("Values are not equal. Expecting '" + int1
+ "', but '" + int2 + "' was provided.");
}

public static void assertFalse(boolean v) {
if (v)
throw new AssertionError("Value is not false");
}

public static void assertTrue(boolean v) {
if (!v)
throw new AssertionError("Value is not true");
}

}




Disclaimer: This message (including attachments) is confidential and may be privileged. If you have received it by mistake please notify the sender by return e-mail and delete this message from your system. Any unauthorized use or dissemination of this message in whole or in part is strictly prohibited. Please note that e-mails are susceptible to change. RxStrategies, Inc. shall not be liable for the improper or incomplete transmission of the information contained in this communication or for any delay in its receipt or damage to your system. RxStrategies, Inc. does not guarantee that the integrity of this communication has been maintained nor that this communication is free from viruses, interceptions or interference.