Application Development and Automation 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:Ā 
Read only

catch exception when dividing zero

Former Member
0 Likes
8,894

As known, you can catch this error by using  'cx_sy_arithmetic_error'.

My question is, is it possible to do it without try catch.

I have tried, if you just use sy-subrc, it would cause a dump.

12 REPLIES 12
Read only

Former Member
0 Likes
6,146

Hi Ming

So why do you want to bypass this standard code?

Regards

Arden

Read only

0 Likes
6,146

Actually, I am telling a develop who came from Java that sy-subrc/raise exception is better than try catch in easily reading.

But there might be some places where sy-subrc cannot be used like dividing zero.

Read only

0 Likes
6,146

Hi Ming,

Otherwise you can place a condition checking whether divisor variable is holding value zero or not. If divisor value is not equal to 0, then only do the division.

Sample:

IF LV_DIVISOR <> 0.

     LV_QUOT = LV_DIVIDEND / LV_DIVISOR.

ENDIF.

Thanks and Regards,

Vijay

Read only

0 Likes
6,146

I would say that this is bad advice for you to give....  TRY-CATCH is the best approach for many scenarios, and with the increased use of exception classes (the recommended exception handling approach for class based development) it becomes more and more common.

Jim

Read only

0 Likes
6,146

I know that try catch is becoming common and it's has more function because it's class-based.

However, since most of methods have exception, especially if you want the logic as strict as possible. Then you will find you code are full of TRY & CATCH. It's not convenience to code and read. Compare this:

In Java:

try

{

call method;

}

catch (xxxException)

{

//handl exception

}

In ABAP

call FM or method.

case sy-subrc.

endcase.

I preffer the ABAP way.

Read only

VenkatRamesh_V
Active Contributor
0 Likes
6,146

Hi Ming,

View the code.

DATA lv_val TYPE P.

CATCH SYSTEM-EXCEPTIONS conversion_errors = 1

                                                   arithmetic_errors   = 5.

lv_val   = 100 / 0.

ENDCATCH.


Hope it helpful.


Regards,

Venkat.

Read only

0 Likes
6,146
Read only

0 Likes
6,146

Hi Eitan,

Thank you for  replying.

Regards,

Venkat.

Read only

0 Likes
6,146

Thank you.

It's the one which I am looking for.

Read only

0 Likes
6,146

And you just ignore the message about obsolete syntax ?

Well.......................... I better say nothing.

Read only

0 Likes
6,146

Let SCI do it for us, with some luck someone activated it at transport request release...

Read only

0 Likes
6,146

I saw it. I am not saying I will use this.

I am just looking for the way that traditional ABAP does this.

Anyway, thank you for you remind.