abap--關於異常的處理
1、異常分類 從sap 6.10開始,abap的異常分為兩類:1)基於異常類的異常,2)非類異常。非類異常又分為系統定義異常(如:被0除異常)和使用者自定義異常(使用者自定義函式中由exception語句定義,raise語句產生的異常)。 異常有的是可以截獲處理,使用者可以截獲做相應處理,系統將可以繼續執行程式。如果使用者不處理,系統將產生錯誤,並停止執行程式。有的異常為不可截獲的錯誤異常,系統將直接產生錯誤,並停止執行程式。
2、異常處理語句
基於類異常相關語句:
a)TRY.
... guarded section
CATCH cx11 ... cx1n [INTO ex1].
... handlers for exceptions cx11 to cx1n
3、異常截獲處理方法
Handling exceptions using/with exception classes 截獲處理方法
data MYREF type ref to CX_SY_ARITHMETIC_ERROR.
data ERR_TEXT type STRING.
data RESULT type I.
try.
RESULT = 1 / 0.
catch cx_sy_arithmetic_error into MYREF.
ERR_TEXT = MYREF->GET_TEXT( ).
endtry.Handling exceptions as catchable runtime errors (向後相容6.10)
4、程式碼樣例 a)RAISING cx1 ... cxn form adbc_exists_view using view_name type dd25l-viewname changing subrc type sy-subrc raising cx_sql_exception.
data: stmt type string, ref type ref to data, stmt_ref type ref to cl_sql_statement, res_ref type ref to cl_sql_result_set, cnt type sy-tabix.
subrc = 4. create object stmt_ref. get reference of view_name into ref. stmt_ref->set_param( ref ). stmt = 'select count(*) from user_views where view_name = ?'. res_ref = stmt_ref->execute_query( stmt ).
* Host-Variable zur Ergebnisaufnahme zuordnen get reference of cnt into ref. res_ref->set_param( ref ).
res_ref->next( ). if cnt = 1. subrc = 0. endif. res_ref->close( ).
endform. form exists_view using view_name type dd25l-viewname changing subrc type sy-subrc. try. perform adbc_exists_view(sdb4fora) using view_name changing subrc. catch cx_sql_exception. subrc = 8. endtry. endform. b)基於類的異常程式碼樣例 5、錯誤與異常類對應關係 Exception group: ARITHMETIC_ERRORS
Class-based Exceptions Definition
*----------------------------------------------------------------------** CLASS class_exception DEFINITION*----------------------------------------------------------------------** All Exception Class must inherit from Class CX_ROOT or its subclass*----------------------------------------------------------------------*CLASS class_exception DEFINITION INHERITING FROM cx_static_check. PUBLIC SECTION. METHODS write_msg.ENDCLASS. "CX_SAMPLE_EXCEPTION DEFINITION
Class-based Exceptions Implementation
*----------------------------------------------------------------------** CLASS class_exception IMPLEMENTATION*----------------------------------------------------------------------*CLASS class_exception IMPLEMENTATION. METHOD write_msg. WRITE / 'Method of Class class_exception'. ENDMETHOD. ":ENDCLASS. "class_exception IMPLEMENTATION
Class main Definition
*----------------------------------------------------------------------** CLASS main DEFINITION*----------------------------------------------------------------------*CLASS main DEFINITION. PUBLIC SECTION.* This method use the class exceptions class_exception* to deal with error. To do this we use the statement RAISING METHODS action RAISING class_exception.ENDCLASS. "main DEFINITION
Class main Implementation
*----------------------------------------------------------------------** CLASS main IMPLEMENTATION*----------------------------------------------------------------------*CLASS main IMPLEMENTATION. METHOD action.* Here we're raising an exception that should be Treated by* exception class class_exception RAISE EXCEPTION TYPE class_exception. ENDMETHOD. "actionENDCLASS. "main IMPLEMENTATION
Defining Objects
DATA o_exception TYPE REF TO class_exception.DATA o_main TYPE REF TO main.
Instance Creation
START-OF-SELECTION. CREATE OBJECT o_main.
Calling Methods
* The statement TRY must be used to define a block that CATCH the exceptions TRY. o_main->action( ).* The Statement CATCH define a block that catches the exceptions of the* exception class class_exception CATCH class_exception. WRITE / 'Exception Caught'. ENDTRY.* The statement TRY must be used to define a block that CATCH the exceptions TRY. o_main->action( ).* The Statement CATCH define a block that catches the exceptions of the* exception class class_exception CATCH class_exception INTO o_exception. WRITE / 'Exception Caught'. o_exception->write_msg( ). ENDTRY.
Exception group: CONVERSION_ERRORS
Exception group: CREATE_DATA_ERRORS
This group contains runtime errors that may occur during the creation of data objects.
Exception group: CREATE_OBJECT_ERRORS
This group contains runtime errors that may occur during the creation of objects.
Exception group: DATA_ACCESS_ERRORS
This group contains runtime errors that may occur during subfield access (with offset/length) to data objects.
Since a subfield access can occur in almost all statements, no keywords can be assigned.
Exception group: DYNAMIC_CALL_METHOD_ERRORS
This group contains runtime errors that may occur during a dynamic method call. These are normally errors that trigger a syntax error if the call is static.
The following keywords are assigned to this exception group:
Exception group: FILE_ACCESS_ERRORS
This group contains runtime errors that may occur during file access. Typical examples are if the system cannot find the file, if no more space is available to write or create the file, or if the authorization to access the file is missing.
Exception group: IMPORT_MISMATCH_ERRORS
This group contains runtime errors that may occur during the import of data
- from the ABAP memory,
- from the database,
- from the SHARED BUFFER, or
- from a file
if the type or the length of the data stored is not identical with that of the target type.
Exception group: LOCALIZATION_ERRORS
This group contains runtime errors that may occur when you switch to another text environment. Typical examples are if the required language is not allowed or if the system wants to switch to a character set which has not been released.
Exception group: REMOTE_CALL_ERRORS
This group contains runtime errors that may occur during calls in remote systems (currently only CALL METHOD). Typical examples are network errors or the unexpected termination of the connection.
Not assigned to an exception group:
6、類異常類樹