7

I am calling the cl_abap_typedescr=>describe_by_name method. It could possibly throw a TYPE_NOT_FOUND exception. I am coming form c# and in c# it would be quite easy to catch such an error. But in ABAP i am not able to catch the exception.

It starts with the fact that I can't use the TYPE_NOT_FOUND exception object in my code. It just does not exist. It continues with the fact that catching CX_ROOT won't help either. It just ignores my try catch clause and crashes:

TRY .
  descr_ref0 ?= cl_abap_typedescr=>describe_by_name('iabc1').
CATCH cx_root.
  BREAK-POINT.
ENDTRY.
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
Florian
  • 5,918
  • 3
  • 47
  • 86

2 Answers2

8

I assume you need the type-casting, so have provided an additional step for it.

This syntax isn't as elegant as the new syntax, but it's the only way I could get your example to compile:

  data descr_ref1 type ref to cl_abap_typedescr.

  call method cl_abap_typedescr=>describe_by_name(
    exporting  p_name         = 'abc1'
    receiving  p_descr_ref    = descr_ref1
    exceptions type_not_found = 1 ).

  if sy-subrc <> 0.
    break-point.
  else.
    descr_ref0 ?= descr_ref1.
  endif. 

edit:

You can tell which type of exceptions is used by looking at the method:

enter image description here

or alternatively you can use the "Pattern" command in the editor to get the correct syntax.

Esti
  • 3,677
  • 8
  • 35
  • 57
  • Well, I am pretty new to abap. I was hoping that i could get around this but well... thanks :) – Florian Jun 03 '15 at 07:17
  • @thefiloe obviously when you write your own methods, you would always use class-based exceptions; but because of backward compatibility, the old stuff will stick around in SAP standard. – Esti Jun 04 '15 at 21:47
5

TYPE_NOT_FOUND is not a class-based exception. You'll need to use the old syntax and assign a non-zero value to each exception, then check SY-SUBRC whether that value reappears.

vwegert
  • 18,371
  • 3
  • 37
  • 55