# How Do I: Handle Errors In a Web Service?

Some web service errors cannot be detected at compile time. For example, your code may attempt to call a method on a null reference or it may invoke a Database control that attempts to execute some invalid SQL. In most cases, an error at run time will cause an exception to be thrown. Unless you add code to handle the exception, this will cause a SOAP fault to be sent back to the client. Unhandled exceptions also cause rollback of a web service's implicit transaction.

You may want to override the default exception handling and perform a custom action, such as logging an error so that an administrator will know the problem occurred.

There are two ways that you can write code to handle exceptions. The first way is to add try/catch blocks around the code in your method. For example, to catch all exceptions that are thrown in your method myMethod, you would write this:

    /**
     * @common:operation
     */
    public void myMethod()
    {
         try
         {
             // [ Normal code for the method. ]
         }
         catch (Exception e)
         {
             // [ Code to handle error cases. ]
         }
    }

If an exception is thrown at any point during the execution of the normal code of the method, the code in the "catch" block will be executed. For further information on standard Java exception handling, see the Java language tutorial at java.sun.com/docs/books/tutorial/essential/exceptions/index.html.

One disadvantage of this approach is that it only works for "myMethod." So if you want to implement exception handling for all of your methods, you have to add try/catch blocks into every method. The second way to handle exceptions overcomes this disadvantage by adding a handler for the "onException" callback of the JwsContext interface. Unlike try/catch blocks, this code will be executed when an exception is thrown (and not caught) in any of the methods of your web service.

To add this general handler for unhandled runtime exceptions, you would add the method context_onException to your JWS class. The following example shows how to use this method to write to a logfile. Notice that the context_onException method must have the exact same arguments as shown in the example:

    import weblogic.jws.util.Logger;
    import com.bea.control.JwsContext;
    ...
    public class MyWebService implements com.bea.jws.WebService
    {
        transient Logger m_logger;

        /** @common:context */
        JwsContext context;

        public void context_onException(Exception ex, String methodName, Object [] args)
        {
            /*
             * Use the WebLogic logger to write the exception's stack trace to the log file.
             */
            m_logger = context.getLogger("LogCategory");
            java.io.StringWriter writer = new java.io.StringWriter();
            ex.printStackTrace(new java.io.PrintWriter(writer));
            m_logger.debug(writer.toString());
            context.finishConversation();
        }
        ...
    }

This method will be called whenever an unhandled exception occurs within the web service, and writes the exception's stack trace to WebLogic Workshop's pre-configured log file. To learn about the pre-configured log files and how to write to them, see workshopLogCfg.xml Configuration File.

## SOAP Faults

When an error is encountered that make it impossible to successfully complete a web service method invocation, you should notify the client by returning a SOAP fault. To lean more about creating and returning SOAP faults to web service clients, see Generating SOAP Faults from a Web Service.

Related Topics

Generating SOAP Faults from a Web Service

Introduction to Java

Default Transactional Behavior in WebLogic Workshop