‎2010 Oct 21 10:43 AM
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 ?
‎2010 Oct 21 10:47 AM
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).
‎2010 Oct 21 10:47 AM
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).
‎2010 Oct 21 10:57 AM
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
‎2010 Oct 21 11:07 AM
Catch the exception in try endtry block and if its caught donot proceed further.
‎2010 Oct 21 11:09 AM
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
‎2010 Oct 21 11:13 AM
Hi Suhas,
In this case Exceptions can be handled in instance constructors also. A sy-subrc check will also work
‎2010 Oct 21 11:27 AM
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
‎2010 Oct 21 11:35 AM
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.
‎2010 Oct 21 11:39 AM
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
‎2010 Oct 21 11:47 AM
Make some mistake :
Of course constructor should have empty body, and the start method is calling within the program which create the object.
‎2010 Oct 21 11:48 AM
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
‎2010 Oct 21 11:49 AM
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.