Skip to content

CX_SY_MOVE_CAST_ERROR

Raised when a CAST operator fails at runtime — the object is not an instance of the target class.

Purpose

CX_SY_MOVE_CAST_ERROR fires when you attempt to narrow (downcast) an object reference and the actual runtime object is not an instance of the target class or any of its subclasses.

Inheritance chain: CX_SY_MOVE_CAST_ERROR → CX_SY_MOVE_CAST → CX_DYNAMIC_CHECK → CX_ROOT

How to catch

Both examples use CL_SALV_TABLE with standard SFLIGHT data — paste and run directly in SE38.

REPORT z_cast_instance_of.

START-OF-SELECTION.

  DATA lt_data TYPE STANDARD TABLE OF sflight.
  SELECT * FROM sflight INTO TABLE @lt_data UP TO 3 ROWS.

  DATA lo_salv TYPE REF TO cl_salv_table.
  cl_salv_table=>factory(
    IMPORTING r_salv_table = lo_salv
    CHANGING  t_table      = lt_data ).

  " Get a column reference — base type is CL_SALV_COLUMN_LIST
  DATA(lo_col) = lo_salv->get_columns( )->get_column( 'CARRID' ).

  " Guard before CAST — no exception risk
  IF lo_col IS INSTANCE OF cl_salv_column_table.
    DATA(lo_table_col) = CAST cl_salv_column_table( lo_col ).
    lo_table_col->set_cell_type( if_salv_c_cell_type=>hotspot ).
    WRITE: / 'Column cast succeeded'.
  ELSE.
    WRITE: / 'Column is not CL_SALV_COLUMN_TABLE — skip cast'.
    WRITE: / 'Actual type:', cl_abap_typedescr=>describe_by_object_ref( lo_col )->get_relative_name( ).
  ENDIF.
REPORT z_cast_trycatch.

START-OF-SELECTION.

  DATA lt_data TYPE STANDARD TABLE OF sflight.
  SELECT * FROM sflight INTO TABLE @lt_data UP TO 3 ROWS.

  DATA lo_salv TYPE REF TO cl_salv_table.
  cl_salv_table=>factory(
    IMPORTING r_salv_table = lo_salv
    CHANGING  t_table      = lt_data ).

  DATA(lo_col) = lo_salv->get_columns( )->get_column( 'CARRID' ).

  TRY.
      DATA(lo_table_col) = CAST cl_salv_column_table( lo_col ).
      lo_table_col->set_cell_type( if_salv_c_cell_type=>hotspot ).
      WRITE: / 'Cast successful'.
    CATCH cx_sy_move_cast_error INTO DATA(lx).
      WRITE: / 'Cast failed:', lx->get_text( ).
      WRITE: / 'Source type:', lx->source_typename.
      WRITE: / 'Target type:', lx->target_typename.
  ENDTRY.

Key attributes

Attribute Type Description
SOURCE_TYPENAME string Runtime type of the source object
TARGET_TYPENAME string Requested target type
Inherited: get_text( ) string Short description

Common pattern: factory results

Many SAP factories return a base class reference that may be different subclasses:

" CL_SALV_COLUMNS_TABLE=>GET_COLUMN returns CL_SALV_COLUMN_LIST
" Only CL_SALV_COLUMN_TABLE has SET_CELL_TYPE — guard before casting
DATA(lo_col) = lo_salv->get_columns( )->get_column( 'CARRID' ).

IF lo_col IS INSTANCE OF cl_salv_column_table.
  CAST cl_salv_column_table( lo_col )->set_cell_type(
    if_salv_c_cell_type=>hotspot ).
ENDIF.

See also

Comments