Application Development and Automation 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: 
Read only

CREATE OBJECT obj and EXCEPTIONS problem

Former Member
0 Likes
5,213

Hi experts,

I have a problem with my program.


CREATE OBJECT obj
  EXPORTING
    ls_record_local = ls_record
  EXCEPTIONS
    error = 1.

When there is no exception everything is fine, but when the sy-subrc = 1, there is no possibility to trigger any other method like


CALL METHOD obj->get_data.

System says that obj is initial and it goes to dump.

Any idea how to prevent this dump How can I have the reference to my object after exception has been throwed ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,132

I bet the exception is raised because the object could not be created. So... it's impossible to call any methods of that object: it doesn't exist.

To prevent your error: make sure you only call the method(s) when you're sure the object exists (no exception raised).

11 REPLIES 11
Read only

Former Member
0 Likes
3,133

I bet the exception is raised because the object could not be created. So... it's impossible to call any methods of that object: it doesn't exist.

To prevent your error: make sure you only call the method(s) when you're sure the object exists (no exception raised).

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
3,132

CREATE OBJECT command creates an instance of the class. If there is a problem in instantiating the class an EXCEPTION is raised & the class doesn't get instantiated.

In this case you can't use the instance attributes / methods of the class.

Try something like this:

CREATE OBJECT obj
  EXPORTING
    ls_record_local = ls_record
  EXCEPTIONS
    error = 1.

IF sy-subrc NE 0.
  MESSAGE 'Error Instantiating the class' TYPE 'E'.
ENDIF.

CALL METHOD obj->method.

Hope i'm clear.

BR,

Suhas

Edited by: Suhas Saha on Oct 21, 2010 3:40 PM

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,132

Catch the exception in try endtry block and if its caught donot proceed further.

Read only

0 Likes
3,132

Hello Keshav,

This is not class based exception. He doesn't need the TRY ... CATCH block to catch these exception. A SY-SUBRC check should suffice.

BR,

Suhas

Read only

0 Likes
3,132

Hi Suhas,

In this case Exceptions can be handled in instance constructors also. A sy-subrc check will also work

Read only

0 Likes
3,132

Hello,

In this case Exceptions can be handled in instance constructors also

I'm sorry i don't get your point. If you could elaborate! Anyway there are some catchable runtime exception which can be caught using the exception class CX_SY_CREATE_OBJECT_ERROR. Some thing like this:

TRY .
    CREATE OBJECT obj
      EXPORTING
        ls_record_local = ls_record
      EXCEPTIONS
        error           = 1.
  CATCH cx_sy_create_object_error INTO lo_exception.
ENDTRY.

BR,

Suhas

Edited by: Suhas Saha on Oct 21, 2010 3:58 PM

Read only

0 Likes
3,132

Thank you all for the responses.

The thing is that I must call some method after exception has been triggered so sy-subrc checking is enough.

I think that in my case following solution should be ok :


CREATE OBJECT obj                 //in constructor there should start method called with EXCEPTIONS clause ...
   EXPORTING
     data = data.

CALL METHOD obj->start
   EXCEPTIONS
      error = 1.
 IF sy-subrc = 1.
     CALL METHOD obj->method .... //because object is still referenced
 ENDIF.

Read only

0 Likes
3,132

NO!!!!!

IF SY_SUBRC EQ 0.

call your method...

ENDIF.

If SUBRC <> 0 then the object doesn't exist.

EDIT: Never mind... responded too fast. IN this example your object does exist (at least that's what i hope)

Edited by: Maen Anachronos on Oct 21, 2010 12:40 PM

Read only

0 Likes
3,132

Make some mistake :

Of course constructor should have empty body, and the start method is calling within the program which create the object.

Read only

0 Likes
3,132

Hello Daniel,

If the object of the class can't be created then the object reference variable is set to it's initial value. Only if the CREATE OBJECT command is successful the SY-SUBRC is set to 0, for other cases it'll have non-zero values. You should check SY-SUBRC after your CREATE OBJECT statement.

Read the F1 help for CREATE OBJECT for better understanding.

Suhas

Read only

0 Likes
3,132
CREATE OBJECT obj                 //in constructor there should start method called with EXCEPTIONS clause ...
   EXPORTING
     data = data.

If this one fails... then you will still get a dump on

CALL METHOD obj->start
   EXCEPTIONS
      error = 1.