Show TOC

Procedure documentationException Handling Locate this document in the navigation structure

 

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 is equipped with three excpetion types: JCO.Exception and two different subclasses that you can call separately when required. 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):

Exception Handling

Syntax Syntax

  1. catch (JCO.AbapException ex) {  
  2.    if (ex.getKey().equalsIgnoreCase("NOT_FOUND")) {       
  3.       System.out.println("Dictionary structure/table not found.");       
  4.       System.exit(1);   
  5.    }   
  6.    else {      
  7.       System.out.println(ex.getMessage());      
  8.       System.exit(1);   
  9.    }
  10. }
  11. catch (JCO.Exception ex) {
  12.       ex.printStackTrace();      
  13.       System.exit(1);   
  14.    }   
  15.    catch (Exception ex) {     
  16.       ex.printStackTrace();      
  17.       System.exit(1);  
  18.    }
End of the code.

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

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.

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.