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: 

Catching an Exception is sometimes not working.

qdwe93
Explorer
0 Kudos
663
data: l_string type string.


try.
perform ztest_form_creat_sqlexception.
catch cx_sy_open_sql_db into data(err1).
l_string = | case A-1: { err1->get_text( ) }|.
catch cx_root into data(err2). " catched here
l_string = | case A-2: { err2->get_text( ) }|.
endtry.
message l_string type 'I' display like 'E'.


try.
perform ztest_form_creat_dynexception.
catch cx_sy_send_dynpro_no_receiver into data(err3). " catched here
l_string = | case B-1: { err3->get_text( ) }|.
catch cx_root into data(err4).
l_string = | case B-2: { err4->get_text( ) }|.
endtry.
message l_string type 'I' display like 'E'.

exit.


form ztest_form_creat_sqlexception.
raise exception type cx_sy_open_sql_db.
endform.

form ztest_form_creat_dynexception.
raise exception type cx_sy_send_dynpro_no_receiver.
endform.

With the code above, I get case A-2 and case B-1.

Is there any reason for having the different catching points with each exceptions?

Because if I cannot know the reason, I need to catch all excaptions with cx_root.

Thanks.

1 ACCEPTED SOLUTION

matt
Active Contributor
588

You need to use raising for non-dynamic exceptions.

form ztest_form_creat_sqlexception raising cx_sy_open_sql_db.

When you run your original program the error message actually tells you this.
case A-2: An exception with the type CX_SY_OPEN_SQL_DB was raised, but was not handled l
ocally or declared in a RAISING clause

I've highlighted the relevant part.

8 REPLIES 8

matt
Active Contributor
588

Please edit your question and used the CODE button to format the code nicely so we can read it.

e.g.

try.
    perform ztest_form_creat_sqlexception.
  catch cx_sy_open_sql_db into data(err1).
    l_string = | case A-1: { err1->get_text( ) }|.
  catch cx_root into data(err2). " catched here<
    l_string = | case A-2: { err2->get_text( ) }|.
endtry.
message l_string type 'I' display like 'E'.

try.
    perform ztest_form_creat_dynexception.
  catch cx_sy_send_dynpro_no_receiver into data(err3). " catched here
    l_string = | case B-1: { err3->get_text( ) }|.
  catch cx_root into data(err4).
    l_string = | case B-2: { err4->get_text( ) }|.
endtry.
message l_string type 'I' display like 'E'.

return. " exit. " Use RETURN rather than EXIT

form ztest_form_creat_sqlexception.
  raise exception type cx_sy_open_sql_db.
endform.

form ztest_form_creat_dynexception.
  raise exception type cx_sy_send_dynpro_no_receiver.
endform.

matt
Active Contributor
589

You need to use raising for non-dynamic exceptions.

form ztest_form_creat_sqlexception raising cx_sy_open_sql_db.

When you run your original program the error message actually tells you this.
case A-2: An exception with the type CX_SY_OPEN_SQL_DB was raised, but was not handled l
ocally or declared in a RAISING clause

I've highlighted the relevant part.

588

Your comment was really helpful.

I finally reached The New Exception Handling Concept in ABAP – Part 5,

which gave me the concept of cx_static_check, cx_dynamic_check, cx_no_check.Thanks.

Sandra_Rossi
Active Contributor
588

x_xo_o0_0 Just to be clear for future visitors, these are the parent types of cx_sy_open_sql_db and cx_sy_send_dynpro_no_receiver (respectively cx_dynamic_check and cx_no_check), hence the result you have observed are as expected.

Sandra_Rossi
Active Contributor
0 Kudos
588

"Catch a dump" is meaningless. You don't catch a dump. A dump is the result of not catching an exception (or of an exception which is not catchable).

Sandra_Rossi
Active Contributor
0 Kudos
588

Look at ERR2 and ERR3, you will start to understand.

qdwe93
Explorer
0 Kudos
588

@sandra.rossi You're right. I've change the title.

qdwe93
Explorer
0 Kudos
588

@matthew.billingham Thanks. It's lovely. 🙂