try-catch语法
try.
try block <code that raises an exception>
catch
catch block <exception handler m>
. . .
. . .
. . .
catch
catch block<exception handler r>
cleanup.
cleanup block <to restore consistent state>
endtry.
捕捉异常
data: result type p length 8 decimals 2,
exref type ref to cx_root,
msgtxt type string.
parameters: num1 type i, num2 type i.
try.
result = num1 / num2.
catch cx_sy_zerodivide into exref.
msgtxt = exref→get_text().
catch cx_sy_conversion_no_number into exref.
msgtxt = exref→get_text().
异常的五个属性
属性 | 说明 |
---|---|
textid | 用于定义异常的不同文本,并且也影响方法get_text的结果 |
previous | 此属性可以存储原始异常,允许您构建异常链 |
get_text | 这将根据异常的系统语言将文本表示作为字符串返回 |
get_longtext | 这会将异常的文本表示的长变体作为字符串返回。 |
get_source_position | 给出引发异常的程序名和行号 |
Demo
report zexceptionsDemo.
parameters num_1 type i.
data res_1 type p decimals 2.
data orf_1 type ref to cx_root.
data txt_1 type string.
start-of-selection.
write: / 'square root and division with:',num_1.
write: /.
try.
if abs(num_1)>150.
raise exception type cx_demo_abs_too_large.
endif.
try.
res_1 = sqrt(num_1).
write: / 'result of square root:', res_1.
res_1 = 1 / num_1.
write: / 'result of division:',rest_1.
catch cx_sy_zerodivide into orf_1.
txt_1 = orf_1→get_text().
cleanup.
clear res_1.
endtry.
catch cx_sy_arithmetic_error into orf_1.
txt_1 = orf_1→get_text().
catch cx_root into orf_1.
txt_1 = orf_1→get_text().
endtry.
if not txt_1 if initial.
write / txt_1.
endif.
write: / 'final result is:', res_1.
square root and division with: 160
the absolute value of number is too high
final result is : 0.00
网友评论