-
Notifications
You must be signed in to change notification settings - Fork 448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Prefer RAISE EXCEPTION NEW to RAISE EXCEPTION TYPE] In defense of RAISE EXCEPTION TYPE #347
Comments
Huh, how weird. I've seen people add logic to the exception constructor to log exceptions, unaware of that apparently not happening in all cases? What happens if parameters are supplied? Are expressions in the actual parameters only evaluated if someone catches the exception with into when using RAISE EXCEPTION TYPE? Also now the "always create exception objects" setting in the debugger makes some more sense. |
I think the passage in the guide is definitely worthy of improvement, but I don't think we should recommend using What should be improved is
Now, as for why we shouldn't recommend As for expectations: I don't know of a single other language with object-oriented exceptions that would have such conditional creation of exceptions, and the ABAP documentation itself seems to think this is a detail people shouldn't really bother with because it doesn't even explain this in any detail. All I could find is the following:
with no elaboration on what exactly "necessary" means. So the vast majority of ABAP programmers has no idea this feature exists, and no non-ABAP programmer would expect it. Let's follow the principle of least surprise and have the guide recommend |
The performance argument in favour of From the performance hit and Marco's tests, I'm guessing there may be a partial instantiation happening internally, so I extended the Here are my performance test results that include
Just on the comment "in properly written applications it should not be necessary. Exceptions are supposed to represent exceptional circumstances, and if they're thrown so often that the instantiation of the exception objects has a measureable impact on performance, the code has much more fundamental problems" I don't think this is always the case, I believe an exception can be a clean way to handle
The presence of an exception gives a hint that we expect the value to be there in most cases, but the performance hit is still surprising. |
Imo there are way cleaner solutions for this exact example than try-catch. row = VALUE #( itab[ f = somevalue ] DEFAULT calculate_and_add_row_to( itab ) ). row = VALUE #( itab[ f = somevalue ] OPTIONAL ).
IF row IS INITIAL.
row = calculate_and_add_row_to( itab )
ENDIF. or even READ TABLE itab WITH KEY f = somevalue INTO row.
IF sy-subrc <> 0.
row = calculate_and_add_row_to( itab ).
ENDIF. Edit: ah you edited a bit and also covered the alternatives in the blog as well. |
@pokrakam I don't think we're disagreeing - if the exception is truly exceptional, then there should be no performance problem and your data(row) = value #( itab[ f = somevalue ] optional ).
if row is initial.
row = calculate_row( somevalue ).
insert row into itab.
endif. Also, the |
I admit it wasn't the best example, The problem with Maybe it's just me but though I understand Anyhow, all of that was a side issue, my main point was the fact that over 80% the performance overhead is still there with It does seem quite odd though that an uninstantiated class should consume so much resource, so maybe there's something that needs tweaking in the ABAP kernel? |
Its more to this point : https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP_de.md#nur-einen-ausnahmetyp-werfen On this point : How about the delegation of concret error messages ? What i miss is the normal request to display the right error message. There is no way i know to delegate the message. You can pass if_t100_message~t100key but you need to replace the attributes in it. So you have object A, witch contains the clear message. You force this as PREVIOUS object to object B which es no error message defined. So to display a message to the user, you need always to iterate among the hierachy and collect all messages from previous object. |
Relevant sections of the style guide
https://github.com/SAP/styleguides/blob/main/clean-abap/CleanABAP.md#prefer-raise-exception-new-to-raise-exception-type
Summary
The current style guide recommends using
RAISE EXCEPTION NEW
overRAISE EXCEPTION TYPE
. However, this overlooks the significant difference in the instantiation of the exception object between these methods. This could potentially impact program behavior. I propose to reconsider this guideline.Description
Greetings,
I'd like to propose a discussion regarding the existing guideline that favors
RAISE EXCEPTION NEW
overRAISE EXCEPTION TYPE
. While I acknowledge thatRAISE EXCEPTION NEW
is indeed shorter and arguably easier to read, I believe that there is a significant aspect that has not been sufficiently addressed.The key difference between these two methods lies in the instantiation of the exception object.
RAISE EXCEPTION NEW
instantiates an exception object immediately, whereasRAISE EXCEPTION TYPE
leaves this to the handler. The latter only creates the exception object when aCATCH
orCLEANUP
clause uses theINTO
addition. Here's an illustration:The reason for this difference can be traced back to the late inclusion of class-based exceptions in the language. To ensure competitiveness and allow for regression-free refactoring, the class-based exceptions were designed to be as efficient as their predecessors. Since object creation can be relatively time-consuming and many exceptions can be handled without creating objects, it was deemed prudent to only create objects when absolutely necessary. The
RAISE EXCEPTION
variant (withoutTYPE
) is meant to pass on existing exceptions to the next program level(s).If exceptions are only used in exceptional situations and not as a rule, the performance may be negligible. But this distinction is of crucial importance if exception constructors are not free of side effects. Although best practices dictate that constructors should not have side effects, this cannot always be guaranteed. Therefore, if not properly understood, the substitution of
RAISE EXCEPTION TYPE
withRAISE EXCEPTION NEW
could potentially modify the behavior of the program.Personally, I would always advise using
RAISE EXCEPTION TYPE
due to its superior capabilities. However, if specifyingEXPORTING
is considered detrimental to readability, the guide should at least caution users about this potential pitfall.I'm eager to hear your thoughts on this suggestion and hope to foster a productive dialogue that will enhance our coding practices.
Examples
Constructors should never have side effects! Please do not imitate! I only provide the following example to illustrate my point.
The text was updated successfully, but these errors were encountered: