< Prev - Examples - Next >

Raise Application Error

Example Description

Important Notes !

PL/SQL Procedure

The procedure 'RAISE_EXCEPTION' shows the complete stacktrace info.
PL/SQL Procedure : RAISE_EXCEPTION
create or replace procedure raise_exception
/**
 * Demonstration of handling user and runtime PL/SQL exceptions.<br>
 * Just have a look at the stacktrace. The call hierarchy of the java methods and PL/SQL stored procedures 
 * are displayed with line numbers.
 */
is
begin
  raise_application_error(-20000, 'just throw an exception displaying this text information');
end raise_exception;

Factory API : Calling the PL/SQL package

Using the static factory to get the remote service.
Java Calling Class : RaiseApplicationErrorFactoryApi.java
package plsql_workbench_examples.factoryapi;

import factory.ExamplesRPCFactory;
import service.RaiseExceptionService;

public class RaiseApplicationErrorFactoryApi {
  public static void main(String[] args) {
    try {
      // getting the service
      RaiseExceptionService service = ExamplesRPCFactory.getRaiseExceptionService();

      // calling the stored procedure
      service.call();
      
      System.err.println("NO - call has to throw a SQLException");
    }
    catch (Exception e) {
      System.out.println(">>> Message :\n" + e.getMessage());
      System.out.println(">>> Stacktrace :");
      e.printStackTrace(System.out);
    }
  }
}

Spring API : Calling the PL/SQL package

Using Spring annotation to inject the service and call the remote service.
Java Calling Class : RaiseApplicationErrorSpringApi.java
package plsql_workbench_examples.springapi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import service.RaiseExceptionService;

@Component
public class RaiseApplicationErrorSpringApi {
  @Autowired
  private RaiseExceptionService raiseExceptionService;

  public static void main(String[] args) {
    // Register Spring Beans, Spring Context and call demo method 
    try (GenericApplicationContext ctx = BaseSpringConfig.getCtx(RaiseApplicationErrorSpringApi.class)) {
      ctx.getBean(RaiseApplicationErrorSpringApi.class).runDemo();
    }
  }

  private void runDemo() {
    try {
      // calling the stored procedure
      raiseExceptionService.call();
      
      System.err.println("NO - call has to throw a SQLException");
    }
    catch (Exception e) {
      System.out.println(">>> Message :\n" + e.getMessage());
      System.out.println(">>> Stacktrace :");
      e.printStackTrace(System.out);
    }
  }
}