Start of Content Area

Procedure documentation Exception Handling  Locate the document in its SAP Library structure

Prerequisites

For exception handling, you need the following classes:

 

·         JCO.Exception: This is the basis class for exceptions.

·         JCO.ConversionException: Subclass for conversion errors.

·         JCO.AbapException: Subclass for exceptions that are thrown in the RFM .

 

Use

SAP JCo throws exceptions as a subclass of RuntimeException. Runtime Exceptions should not be integrated or defined in the signature of the method. There can be advantages to doing this, but it is also dangerous. Local error handling is less problematic because the program still knows in which context the error has occurred. SAP therefore recommends that you always use try/catch in the code, even if the compiler does not display this.

 

SAP JCo also provides three exception types:  JCO.Exception  and two subclasses, which you can call separately if necessary. JCO.Exception also provides a getGroup() method, which you can use to distinguish between different error types. For a list of the groups, see the corresponding Javadoc in the installation directory docs.

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

JCO.AbapException occurs if the ABAP code that you have called throws an exception. The diagram below shows how you can distinguish between different error types (the example shows the use of the DDIF_FIELDINFO_GET, which can be used to query structure metadata or table metadata. If an invalid name is transferred, this RFM throws the exception NOT_FOUND):

Syntax documentation

 Exception Handling

catch (JCO.AbapException ex) {

 

   if (ex.getKey().equalsIgnoreCase("NOT_FOUND")) {

      

      System.out.println("Dictionary structure/table not found.");

      

      System.exit(1);

  

   }

  

   else {

     

      System.out.println(ex.getMessage());

     

      System.exit(1);

  

   }

 

}

 

catch (JCO.Exception ex) {

 

 

 

      ex.printStackTrace();

     

      System.exit(1);

  

   }

  

   catch (Exception ex) {

    

      ex.printStackTrace();

     

      System.exit(1);

 

   }

 

 

 

There are three “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 catch clause 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 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 Content Area