Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Error handling with OPEN DATASET

Former Member
0 Kudos

Hi,

I have a question about the usage of error handling in OPEN DATASET

I can user MESSAGE msg option with OPEN DATASET

or

I can use TRY .... CATCH .. ENDTRY. statement.

1. Can I use both these ? or If I use one option, Is it true that I should not use the other option?

2. What is the difference between these two options?

Please let me know. Your help is appreciated.

Thank you,

Surya

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Why not use sy-subrc?

if sy-subrc is ne 0

" then you have a problem

You can use more specific number if you wish... I use and it tends to work well

11 REPLIES 11

Former Member
0 Kudos

hey..

check this..

OPEN DATASET dset FOR access IN mode MESSAGE msg .

If an error occurs when a fileis opened, the corresponding operating system message is assigned to the data object msg. A character-type variable can be entered for msg.

Regards,

KC

martinborda
Explorer

Hi Surya,

I always use this code to handle errors in OPEN DATASET, both those that gets a SY-SUBRC NE 0 and those which could generate a dump (TRY...CATCH...ENDTRY):


CONSTANTS: c_error TYPE sy-msgty VALUE 'E'.

PARAMETERS: p_path TYPE admi_path.

START-OF-SELECTION.
 
...
 
  DATA: l_message   TYPE string,
        o_exception TYPE REF TO cx_root.

  TRY.

      OPEN DATASET p_path
           FOR OUTPUT
           IN TEXT MODE ENCODING DEFAULT
           MESSAGE l_message.

*     Error
      IF sy-subrc IS NOT INITIAL.

        MESSAGE l_message
           TYPE c_error.

      ENDIF.

*   Error
    CATCH cx_root
      INTO o_exception.

*     Gets error message
      CALL METHOD o_exception->if_message~get_text
        RECEIVING
          result = l_message.

      MESSAGE l_message
         TYPE c_error.

  ENDTRY.
 
...
 
  CLOSE DATASET p_path. 

martinborda
Explorer
0 Kudos

Regarding the difference between SY-SUBRC errors and EXCEPTIONS errors in OPEN DATASET, they are just two different ways of getting (different) possible errors, so you must take both into account.

This is from OPEN DATASET SAP help:

Return Value

sy-subrc Description 
0 File was opened. 
8 Operating system could not open file. 
 

Exceptions 
Catchable Exceptions 

CX_SY_FILE_OPEN 

Cause: File is already open (only in Unicode programs) 
Runtime Error: DATASET_REOPEN 

CX_SY_CODEPAGE_CONVERTER_INIT 

Cause: The desired conversion is not supported. (Due to specification of invalid code page or of language not supported in the conversion, with SET LOCALE LANGUAGE.) 
Runtime Error: CONVT_CODEPAGE_INIT (catchable) 

CX_SY_CONVERSION_CODEPAGE 

Cause: Internal error in the conversion. 
Runtime Error: CONVT_CODEPAGE (catchable) 

CX_SY_FILE_AUTHORITY 

Cause: No authorization for access to file 
Runtime Error: OPEN_DATASET_NO_AUTHORITY (catchable) 

Cause: Authorization for access to this file is missing in OPEN DATASET with addition FILTER. 
Runtime Error: OPEN_PIPE_NO_AUTHORITY (catchable) 

CX_SY_PIPES_NOT_SUPPORTED 

CX_SY_TOO_MANY_FILES 

Cause: Maximum number of open files exceeded. 
Runtime Error: DATASET_TOO_MANY_FILES (catchable) 


Non-Catchable Exceptions 

Cause: You tried to open a pipe that is already open. 
Runtime Error: DATASET_PIPE_POSITION

0 Kudos

Hi Martin,

Thaks for your quick reply. This helps me.

I have a final question. What will be the value of SY-SUBRC in case of an exception?

Thank you,

Surya.

former_member156446
Active Contributor
0 Kudos

Former Member
0 Kudos

Why not use sy-subrc?

if sy-subrc is ne 0

" then you have a problem

You can use more specific number if you wish... I use and it tends to work well

0 Kudos

Keith,

As you said, some errors will just generate a SY-SUBRC NE 0, but not trigger any exception.

These errors are easily "catchable" with:


  DATA: l_message   TYPE string.

      OPEN DATASET p_path
           FOR OUTPUT
           IN TEXT MODE ENCODING DEFAULT
           MESSAGE l_message.
 
*     Error
      IF sy-subrc IS NOT INITIAL.
 
        MESSAGE l_message
           TYPE c_error.
 
      ENDIF.

But another errors will trigger an exception, and if you don't catch them, they will show a nice dump in your program.

You catch them with:


  DATA: l_message   TYPE string,
        o_exception TYPE REF TO cx_root.

  TRY.
 
      OPEN DATASET p_path
           FOR OUTPUT
           IN TEXT MODE ENCODING DEFAULT.
 
...
 
*   Error
    CATCH cx_root
      INTO o_exception.
 
*     Gets error message
      CALL METHOD o_exception->if_message~get_text
        RECEIVING
          result = l_message.
 
      MESSAGE l_message
         TYPE c_error.
 
  ENDTRY.

That's why in the code I posted, you catch both types of errors that OPEN DATASET can generate.

0 Kudos

Hi Martin,

Is it Ok to use like the following in Get error message?

  • Error

CATCH cx_root

INTO o_exception.

  • Gets error message

l_message = o_exception->get_text( ).

MESSAGE l_message

TYPE c_error.

Thank you,

Surya

0 Kudos

Yes, it's the same, I just copied and pasted the method name from SE24.

Don't forget to take SY-SUBRC errors into account too.

0 Kudos

Thank you!

former_member156446
Active Contributor
0 Kudos
DATA:
  gr_err TYPE REF TO cx_root,
  gs_msg TYPE string.
*
TRY.
  OPEN DATASET xxx FOR INPUT IN TEXT MODE ENCODING DEFAULT.
    CATCH cx_root INTO gr_err.
      gs_msg = gr_err->get_text( ).
ENDTRY.
*
IF  gs_msg IS NOT INITIAL.
    WRITE: /001 gs_msg.
ENDIF.

From the link I posted before.