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: 

Local exception class in global class

dirk_wittenberg
Contributor
0 Kudos

Hi,

in SE24, tab "local types", the comment given by SAP says:

*"* use this source file for any type declarations (class
*"* definitions, interfaces or data types) you need for method
*"* implementation or private method's signature

Now I've defined a local exception class in this tab:

CLASS lcx_my_exception DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.

So far no error occurs.

But when trying to use it in the signature of a private! class, it is not known and I'm asked wether I like to create it as a global exception class.

Why isn't the exception class recognized?

My mistake or a bug?

Regards,

Dirk

1 ACCEPTED SOLUTION

wol
Active Participant
0 Kudos

Hello Dirk,

i cant follow that problem.

I tried the following:

  • Created a global class.
  • Entered the following under Local Types:

*"* use this source file for any type declarations (class
*"* definitions, interfaces or data types) you need for method
*"* implementation or private method's signature

CLASS lcl_cx DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.                    "lcl_cx DEFINITION


  • Declared the exception in a private testmethod with the following code:

METHOD i_raise_it.

  RAISE lcl_cx.

ENDMETHOD.

No error occurs.

Even if I make the method public, it works.

I did this in ECC 6.0.

Maybe this works not until a special version?

Regards,

Stefan Wolf

9 REPLIES 9

matt
Active Contributor
0 Kudos

Your mistake. Class based exceptions for methods must be globally defined exceptions. It'd be kind of nice if you could use private ones though.

Yes, it would be nice.

If the method is private it's called only inside the class. Why have exceptions thrown by this method be known outside? It's inconsistent in my optionion.

Regards,

Dirk

wol
Active Participant
0 Kudos

Hello Dirk,

i cant follow that problem.

I tried the following:

  • Created a global class.
  • Entered the following under Local Types:

*"* use this source file for any type declarations (class
*"* definitions, interfaces or data types) you need for method
*"* implementation or private method's signature

CLASS lcl_cx DEFINITION INHERITING FROM cx_static_check.

ENDCLASS.                    "lcl_cx DEFINITION


  • Declared the exception in a private testmethod with the following code:

METHOD i_raise_it.

  RAISE lcl_cx.

ENDMETHOD.

No error occurs.

Even if I make the method public, it works.

I did this in ECC 6.0.

Maybe this works not until a special version?

Regards,

Stefan Wolf

wol
Active Participant
0 Kudos

Hello Dirk,

did you solve the problem?

Could you share the crux of the matter with us?

Regards,

Stefan Wolf

0 Kudos

Hi Stefan,

sorry not yet, but I will keep you informed.

Currently there's not much time for experiments .

Regards,

Dirk

wol
Active Participant
0 Kudos

OT: Sounds a bit like "Inbetriebnahme"...

custodio_deoliveira
Active Contributor
0 Kudos

Hi Dirk,

I've been trough the same issue and "solved" it by declaring the super class (CX_DYNAMIC_CHECK in my case) in the signature of my private method.

Works fine for me.

Cheers,

Custodio

0 Kudos

Hi Custodio,

thanks for your reply.

I tested your solution and it worked. It's a possible workaround of course but it makes the code a bit "unclear" beause you can't see in the signature which exception can actually be thrown.

Regards,

Dirk

0 Kudos

I use eclipse to write this:

In the sample class below, I use a private method to process Fibonacci number called in the public class " get_fib".

hope this answers your question.

In the LOCAL TYPES

*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations


CLASS lcx_input IMPLEMENTATION.


 METHOD constructor.


 super->constructor( ).
 m_message = i_message.


 ENDMETHOD.


 METHOD get_message.


  r_message = m_message.


 ENDMETHOD.




ENDCLASS.

In the CLASS-RELEVANT LOCAL TYPES

*"* use this source file for any type of declarations (class
*"* definitions, interfaces or type declarations) you need for
*"* components in the private section


CLASS lcx_input DEFINITION
INHERITING FROM cx_static_check FINAL.


 PUBLIC SECTION.
 METHODS: constructor IMPORTING i_message TYPE string,
          get_message RETURNING VALUE(r_message) TYPE string.


 PRIVATE SECTION.
 DATA: m_message TYPE string.


ENDCLASS.

In the GLOBAL CLASS

CLASS ycl_fibonacci_number DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .


  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
    METHODS get_fib IMPORTING i_index             TYPE i
                    RETURNING VALUE(r_fib_number) TYPE i
                    RAISING   /iwbep/cx_mgw_busi_exception.
  PROTECTED SECTION.
  PRIVATE SECTION.
    METHODS process_fib IMPORTING i_index             TYPE i
                        RETURNING VALUE(r_fib_number) TYPE i
                        RAISING   lcx_input.
ENDCLASS.






CLASS ycl_fibonacci_number IMPLEMENTATION.
  METHOD get_fib.


    TRY.
        r_fib_number = process_fib( i_index ).


      CATCH lcx_input INTO DATA(input_ex).


        RAISE EXCEPTION TYPE /iwbep/cx_mgw_busi_exception
          EXPORTING
            textid            = /iwbep/cx_mgw_busi_exception=>business_error_unlimited
            message_unlimited = input_ex->get_message( ).


    ENDTRY.


  ENDMETHOD.


  METHOD if_oo_adt_classrun~main.


  ENDMETHOD.


  METHOD process_fib.


    IF i_index < 0.
      RAISE EXCEPTION TYPE lcx_input
        EXPORTING
          i_message = 'Input can not be less than zero'.
    ENDIF.




    IF i_index = 0.
      r_fib_number = 0.


    ELSEIF i_index = 1.
      r_fib_number = 1.


    ELSE.
      r_fib_number = process_fib( i_index - 1 ) + process_fib( i_index - 2 ).
    ENDIF.




  ENDMETHOD.


ENDCLASS.