Raise Exception with custom message in ABAP

心已入冬 提交于 2019-12-12 14:42:54

问题


I want the most simple way to raise an exception with a custom message in ABAP.

It would be great, if this could be done with few characters as possible. If possible on one line.

I want this for debugging, not for running production code.

Background: In the past I used the Python programming language. I like to debug without a debugger. I put some assert 0, myvar lines in to the code and execute the code. For me this feels faster then tradition debugger with stepping over or into code lines. I am searching for an equivalent to assert 0, mvar in ABAP.

Raising an exception is just the strategy I have on my mind at the moment. Every other strategy to get to the overall goal is welcome.

I am coding a function which gets called via RFC.


回答1:


I found that this works:

message my_string_var type 'E'.

I add the MESSAGE TYPE 'E' only temporary into the code. I test my ABAP code from outside (via PyRFC).




回答2:


There is ASSERT statement in ABAP that you could use in a similar way to your Python case.

Here is a docu:

ASSERT




回答3:


As you already found out, MESSAGE text TYPE 'E' will let you throw arbitrary messages that abort program execution. You could also choose 'A' as a type. However, do not use this to test your code.

There are several reasons for this:

  1. As the documentation of MESSAGE explicitly states, MESSAGE is intended exclusively to interact with the user and should only be used in the layor of your code closest to the user interface. The exact behaviour of messages is difficult to predict, since it depends on the processing mode in which your code is executed, cf. the documentation of message behavior. If you absolutely must, use ASSERT instead, which has much more consistent behavior.

  2. You should not alter your production code for testing purposes. Test code and production code should be as disjoint as possible, since otherwise you run the risk of accidentally leaving code purely intended for testing in your production code. These days, ABAP offers a powerful unit testing framework in the form of ABAP Unit. The assertion methods of cl_abap_unit_assert all offer a msg parameter with which you can definie very specific error messages for failing tests. This allows you to do detailed testing without having to alter production code (if you designed it to be testable).

  3. Both MESSAGE and ASSERT produce short dumps. In development systems, this is usually not seen as that bad, but you should avoid producing short dumps if you can, since it makes it harder for readers of e.g. the ST22 logs to distinguish true unforeseen error situations in mature code from failed tests during active development. A failing unit test does not dump, and is just as useful.



来源:https://stackoverflow.com/questions/52661797/raise-exception-with-custom-message-in-abap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!