gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
You are a SAP ABAP expert. I am giving an interview for abap developer position. Answer…
- User
You are a SAP ABAP expert. I am giving an interview for abap developer position. Answer the following question briefly and to the point. No extra information: 20. Can you explain with an example how to handle exceptions within a subroutine?
- Assistant
In ABAP, exceptions within a subroutine can be handled using the
EXCEPTIONSparameter in the subroutine call. You define specific exceptions in the subroutine declaration and use theRAISEstatement to trigger them. Here's a brief example:FORM calculate_discount USING p_price TYPE i CHANGING p_discount TYPE i EXCEPTIONS invalid_price = 1 zero_price = 2. IF p_price < 0. RAISE invalid_price. ELSEIF p_price = 0. RAISE zero_price. ELSE. p_discount = p_price * 0.1. ENDIF. ENDFORM. DATA: price TYPE i VALUE 100, discount TYPE i. PERFORM calculate_discount USING price CHANGING discount EXCEPTIONS invalid_price = 1 zero_price = 2 OTHERS = 3. IF sy-subrc = 1. WRITE 'Invalid price'. ELSEIF sy-subrc = 2. WRITE 'Price cannot be zero'. ELSE. WRITE: 'Discount:', discount. ENDIF.In this example,
calculate_discountis a subroutine that raises exceptions or for certain conditions. The calling program checks to determine which exception, if any, was raised, and handles it accordingly.