Show TOC

Procedure documentationException Handling Locate this document in the navigation structure

Prerequisites

For exception handling, you need the following classes:

  • JCoException: This is the basis interface for exceptions.

    • ABAPException: Special class for errors displayed in the RFM .

      AbapException occurs if the ABAP code that you have called throws an exception.

  • JcoRuntimeException: Basis class for runtime exceptions.

    • ConversionException: Special class for conversion errors.

      ConversionException is always thrown if you call a getter or setter method that requests a conversion, and this conversion fails.

    • XMLParserException: Used with errors in the XML parser.

      XMLParserException is displayed by the XML parser if errors occur in RFC communication when using complex parameters.

SAP JCo throws exceptions as a subclass of JCoException or JCoRuntimeException.

Note Note

In contrast to JCo releases <3.0, Runtime Exceptions are only thrown in a few cases.

End of the note.

An exception is normally thrown as a 'checked' exception, that is as an underclass of JCoException .

However, when using these 'normal' exceptions you must always decide whether an exception is to be handled explicitly or if it is to be added to the throws clause.

Note Note

It is therefore recommended that you use try/catch in the code.

End of the note.

Syntax Syntax

Exception Handling – Option 1

  1.  public void executeFunction(JCO.Function function, String where)
  2.     throws JCoException
  3. {
  4.     JCoDestination destination=JCoDestinationManager.getDestination(where);
  5.     function.execute(destination);
  6. }    
End of the code.

Syntax Syntax

Exception Handling – Option 2

  1. public int executeFunction(JCO.Function function, String where)
  2. {
  3.     int returnCode=0;
  4.     try
  5.     {
  6.         JCoDestination destination=JCoDestinationManager.getDestination(where);
  7.         function.execute(destination);
  8.      }
  9.     catch (AbapException ex) //Klausel 1
  10.     {
  11.         handleAbapException(function, ex);
  12.         returnCode=1;
  13.     }
  14.     catch (JCoException ex) //Klausel 2
  15.     {
  16.         logSystemFailure(ex);
  17.         returnCode=2;
  18.     }
  19.     catch (JCoRuntimeException ex) //Klausel 3
  20.     {        
  21.     }
  22.     catch (Exception ex) //Klausel 4
  23.     {
  24.     }
  25.     return returnCode;
  26. }
End of the code.

There are four catch clauses:

  • In the first catch clause you use the method getKey() to access the exception string returned by the SAP system. If this is NOT_FOUND, a specific text is output, for all other exceptions, use getMessage to generate a suitable text. In this way you can distinguish between different ABAP exceptions that you want to handle in a specific way, and all others that are handled generically. Because all exception strings are defined in SAP, you already know them in advance.

  • The second and third catch clauses refers to all other JCo-relevant problems. These include conversion errors and other errors that occur in SAP JCo.

  • The third clause refers to all other exceptions that may have occurred in your code.

    Note Note

    Note that this is also an example description. Depending on the concrete requirements of your code it can also be necessary to adjust the error handling.

    End of the note.
More Information

You can find a detailed description of the individual exception classes in JCo Javadoc in the installation directory docs.