1

My java code calls a procedure and if there is any exception it gets caught in the java SQLEXCEPTION. Everything works fine if there is an exception that causes the procedure to stop but if the exception does not stop the procedure java does not display the error which we want to log. Here is an example:

Procedure:

create or replace procedure test_jdbc(Table_name IN VARCHAR2) is

  v_sql VARCHAR2(50);
  cursor c_test is
    select employee_id, employee_num from employee where rownum < 11;
  v_test c_test%rowtype;
BEGIN

  for v_test in c_test loop
    begin
      dbms_output.put_line(v_test.employee_id || ' - ' ||
                           v_test.employee_num);
      dbms_output.put_line('c_test%rowcount - ' || c_test%rowcount);
      if c_test%rowcount = 8 then
        v_sql := v_test.employee_id / 0;
      end if;
    exception
      when others then

        dbms_output.put_line(sqlerrm);

    end;

  end loop;

end test_jdbc;

This when run gives the following output:

0 - 1
c_test%rowcount - 1
0 - 2
c_test%rowcount - 2
0 - 3
c_test%rowcount - 3
0 - 4
c_test%rowcount - 4
0 - 5
c_test%rowcount - 5
0 - 6
c_test%rowcount - 6
0 - 7
c_test%rowcount - 7
0 - 8
c_test%rowcount - 8
ORA-01476: divisor is equal to zero
0 - 9
c_test%rowcount - 9
0 - 10
c_test%rowcount - 10

PL/SQL procedure successfully completed

Here is my java code that calls the procedure:

String insertStoreProc = "{call test_jdbc(?)}";

        try {
            dbConnection = getDBConnection();
            callablestatement = dbConnection.prepareCall(insertStoreProc);

            callablestatement.setString(1, "Employee");

            // execute select SQL stetement

            callablestatement.execute();
            System.out.println("Procedure Complete!");




        } catch (SQLException e) {

            e.printStackTrace(System.err);
            System.err.println("SQLState: " +
                ((SQLException)e).getSQLState());

            System.err.println("Error Code: " +
                ((SQLException)e).getErrorCode());

            System.err.println("Message: " + e.getMessage());



        }

However my java does not display the ORA-01476: divisor is equal to zero message and thus I cannot log it. If however there is an exception like say table not found that causes the procedure to exit the java code does display it. How can I log the ORA-01476 error?

2 Answers 2

3

Actually you aren't throwing exceptions, you are just outputing them with dbms_output package.

begin
  -- my stuff
when others then
  dbms_output.put_line(sqlerrm); -- here is just a output, procedure will continue
end;

Try this code (with raise):

begin
  -- my stuff
when others then
  dbms_output.put_line(sqlerrm);
  raise;
end;

Then you will see some error that happened in the block with SQLException

2
  • Well actually raise is going to help me get the exception in java but my client wanted to know if there was a way we could capture the error message if it hit the exception block and an exception is not raised and the procedure is allowed to continue. The way they do it now is by passing an output parameter to java where the `dbms_output.put_line(sqlerrm)' is present to log it.
    – Eosphorus
    Mar 29, 2012 at 16:38
  • So I guess thats the only way to do it. If we dont raise an exception and we want java to know it pass the error thru an out parameter or raise an exception and have SQLEXception catch it. Is there any other way not involving out parameter?
    – Eosphorus
    Mar 29, 2012 at 16:38
2

You are handling exception in Oracle stored procedure.

That is why is not propagated to the client.

My advice is to remove the exception block in Oracle procedure or add RAISE_APPLICATION_ERROR()

exception when others then

    dbms_output.put_line(sqlerrm);
    RAISE_APPLICATION_ERROR(-21000,"Oops division by zero ") 
end;
1
  • 4
    Or just put a line containing RAISE; after the dbms_output in the exception handler. That should re-raise the existing exception to the next outer-most handler. Mar 29, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.