abap中的异常处理_第1页
abap中的异常处理_第2页
abap中的异常处理_第3页
abap中的异常处理_第4页
abap中的异常处理_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、中的异常处理 作者:dyq_972提交日期:2008-4-15 9:39:00 O|分类:| 访问量:895在平时的ABAP开发中,需要捕获的异常通常为两种,一种是执行 SQL比 如主键重复,INSERT语句字段类型不匹配等。还有就是 RFC的通信错误,比如 不能进行远程连接等。通常可以这么处理:1. 数据库异常:DATA: lcx_error TYPE REF TO cx_root. cx_sy_sql_error .cx_sy_ope n_sql_db.DATA i_tab LIKE TABLE OF spfli WITH HEADER LINE.DATA err_text TYPE c

2、LENGTH 1000.TRY.INSERT spfli FROM TABLE i_tab.CATCH cx_sy_open_sql_db INTO lcx_error.err_text = lcx_error-get_text().得至 U错误信息sy-subrc = 4. 改变系统变量ENDTRY.IF sy-subrc 0.WRITE :/ 数据更新失败!.WRITE err_text.ROLLBACK WORK.ELSE.WRITE :/ 数据更新成功!.COMMIT WORK.ENDIF.2. RFC异常:TRY.CALL FUNCTION Z_DAQ_CALL_JCO DESTIN

3、ATION ZJCOSERVER_DAQ EXPORTINGpv_empid = l_empidIMPORTING pv_rlt = l_rltTABLESit_spfli = i_tabEXCEPTIONSsystem_failure = 1 MESSAGE err_text com muni cati on _failure = 2 MESSAGE err_text. ENDTRY.IF SY-SUBRC 0.WRITE: / 调用 * 失败! , err_text.ENDIF.详细的信息可以参加下面的内容: 1、异常分类从 sap 6.10 开始, abap 的异常分为两类: 1) 基于

4、异常类的异常, 2) 非类异常。 非类异常又分为系统定义异常(如:被 0 除异常)和用户自定义异常 (用户自定 义函数中由 exception 语句定义, raise 语句产生的异常 ) 。异常有的是可以截获处理, 用户可以截获做相应处理, 系统将可以继续执行程序。 如果用户不处理, 系统将产生错误, 并停止执行程序。 有的异常为不可截获的错 误异常,系统将直接产生错误,并停止执行程序。2、异常处理语句 基于类异常相关语句:a) TRY. guarded sectionCATCH cx11 . cx1n INTO ex1. handlers for exceptions cx11 to cx1

5、nCATCH cx21 . cx2m INTO ex2. handlers for exceptions cx21 bis cx2m. other handlersCLEANUP. cleanup blockENDTRY.b) RAISE EXCEPTION TYPE class.c) RAISING cx1 . cxn非类异常相关语句:a)catch system-exceptions ARITHMETIC_ERRORS = 4.endcatch.c) raise (In function or method)3、异常截获处理方法Handling exceptions using/with

6、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)此异常处理sap建议使用try.endtry代替(错误和

7、异常类对应关系参见第5部分)。data RESULT type I.catch system-exceptions ARITHMETIC_ERRORS = 4.RESULT = 1 / 0.endcatch.if SY-SUBRC = 4.endif.4、代码样例a)RAISING cx1 . cxnform adbc_exists_view using view_name type dd25l-viewnamechanging subrc type sy-subrc raising cx_sql_exception.data: stmt type string,ref type ref to

8、 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 ).*

9、 Host-Variable zur Ergebnisaufnahme zuordnenget 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_

10、name changing subrc.catch cx_sql_exception.subrc = 8.endtry. endform.b)基于类的异常代码样例5、错误与异常类对应关系 Exception group: ARITHMETIC_ERRORSClass-based Exceptions Definition -* CLASS class_exceptionDEFINITION* All Exception Class must inherit from Class CX_ROOoTr its subclass*CLASS class_exception DEFINITION IN

11、HERITING FROM cx_static_check. PUBLIC SECTION. METHODS write_msg.ENDCLASS. CX_SAMPLE_EXCEPTION DEFINITIONClass-based Exceptions Implementation -* CLASS class_exceptionIMPLEMENTATION*CLASS class_exception IMPLEMENTATIONM.ETHOwDrite_msg.WRITE / Method of Class class_exception. ENDMETHOD. :ENDCLASS. cl

12、ass_exception IMPLEMENTATIONClass main Definition-* CLASS mainDEFINITION*CLASS main DEFINITION. PUBLICSECTION.*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 DEFINITIONClass main

13、Implementation-* CLASS mainIMPLEMENTATION*CLASS main IMPLEMENTATION. METHOD action.* Here were raising an exception that should be Treated by* exception class class_exception RAISE EXCEPTION TYPE class_exception. ENDMETHOD. actionENDCLASS. main IMPLEMENTATIONDefining ObjectsDATAo_exception TYPEREFTO

14、class_exception.DATA o_main TYPEREFTOmain. Instance CreationSTART-OF-SELECTION. CREATE OBJECT o_main.Calling Methods* The statement TRYmust be used to define a block that CATCHthe exceptions TRY. o_main-action( ).* The Statement CATCHdefine a block that catches the exceptions of the* exception class

15、 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 CATCHclass_exception IN

16、TOo_exception. WRITE/ Exception Caught. o_exception-write_msg( ). ENDTRY.(Associated superclass: CX_SY_ARITHMETIC_ERROR)BCD_ZERODIVIDE CX_SY_ZERODIVIDE COMPUTE_FLOAT_ZERODIVIDE CX_SY_ZERODIVIDE COMPUTE_INT_ZERODIVIDE CX_SY_ZERODIVIDE ADDF_INT_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW BCD_OVERFLOW CX_SY_ARI

17、THMETIC_OVERFLOW COMPUTE_BCD_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_COSH_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_EXP_RANGE CX_SY_ARITHMETIC_OVERFLOW COMPUTE_FLOAT_DIV_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_FLOAT_MINUS_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_FLOAT_PLUS_OVERFLOW CX_SY_AR

18、ITHMETIC_OVERFLOW COMPUTE_FLOAT_TIMES_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_INT_ABS_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_INT_DIV_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_INT_MINUS_OVERFLOW CX_SY_ARITHMETIC_OVERFLOWCOMPUTE_INT_PLUS_OVERFLOW CX_SY_ARITHMETIC_OVERFLOWCOMPUTE_INT_TIMES_OVER

19、FLOW CX_SY_ARITHMETIC_OVERFLOWCOMPUTE_LOG10_ECOMPUTE_LOG_ERROR CX_SY_ARITHMETIC_OVERFLOW, CX_SY_ARG_OUT_OF_DOMAINCOMPUTE_MATH_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_POW_RANGE CX_SY_ARITHMETIC_OVERFLOW COMPUTE_SINH_OVERFLOW CX_SY_ARITHMETIC_OVERFLOW COMPUTE_ACOS_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COM

20、PUTE_ASIN_DOMAIN CX_SY_ARG_OUT_OF_DOMAINCOMPUTE_ATAN_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_COSH_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_COS_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_EXP_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_MATH_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_POW_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN

21、 COMPUTE_SINH_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_SIN_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_SQRT_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_TANH_DOMAIN CX_SY_ARG_OUT_OF_DOMAIN COMPUTE_COS_LOSS CX_SY_PRECISION_LOSS COMPUTE_MATH_LOSS CX_SY_PRECISION_LOSS COMPUTE_SIN_LOSS CX_SY_PRECISION_LOSS COMPUTE_T

22、AN_LOSS CX_SY_PRECISION_LOSS BCD_FIELD_OVERFLOW CX_SY_CONVERSION_OVERFLOWException group: CONVERSION_ERRORS(Associated superclass: CX_SY_CONVERSION_ERROR)BCD_FIELD_OVERFLOW CX_SY_CONVERSION_OVERFLOW CONVT_OVERFLOW CX_SY_CONVERSION_OVERFLOW CONVT_CODEPAGE CX_SY_CONVERSION_CODEPAGE CONVT_NO_NUMBER CX_

23、SY_CONVERSION_NO_NUMBER BCD_OVERFLOW CX_SY_ARITHMETIC_OVERFLOWException group: CREATE_DATA_ERRORS(Associated superclass: CX_SY_CREATE_ERROR)This group contains runtime errors that mayoccur during the creation of data objects.CREATE_DATA_UNKNOWN_TYPE CX_SY_CREATE_DATA_ERROR CREATE_DATA_NOT_ALLOWED_TY

24、PE CX_SY_CREATE_DATA_ERROR CREATE_DATA_LEN_NOT_ALLOWED CX_SY_CREATE_DATA_ERROR CREATE_DATA_ILLEGAL_LENGTH CX_SY_CREATE_DATA_ERROR CREATE_DATA_ILLEGAL_DECIMALS CX_SY_CREATE_DATA_ERROR CREATE_DATA_ILLEGAL_INIT_SIZE CX_SY_CREATE_DATA_ERRORException group: CREATE_OBJECT_ERRORS(Associated superclass: CX_

25、SY_CREATE_ERROR)This group contains runtime errors that mayoccur during the creation of objects.CREATE_OBJECT_CLASS_NOT_FOUND CX_SY_CREATE_OBJECT_ERRORCREATE_OBJECT_CLASS_ABSTRACT CX_SY_CREATE_OBJECT_ERRORCREATE_OBJECT_CREATE_PRIVATE CX_SY_CREATE_OBJECT_ERRORCREATE_OBJECT_CREATE_PROTECTED CX_SY_CREA

26、TE_OBJECT_ERRORException group: DATA_ACCESS_ERRORS(Associated superclass: CX_SY_DATA_ACCESS_ERROR)This group contains runtime errors that may occur during subfield access (with offset/length) to data objects.DATA_OFFSET_NEGATIVE CX_SY_RANGE_OUT_OF_BOUNDS DATA_LENGTH_NEGATIVE CX_SY_RANGE_OUT_OF_BOUND

27、SDATA_LENGTH_0 CX_SY_RANGE_OUT_OF_BOUNDS DATA_LENGTH_TOO_LARGE CX_SY_RANGE_OUT_OF_BOUNDS DATA_OFFSET_LENGTH_TOO_LARGE CX_SY_RANGE_OUT_OF_BOUNDS REFI_WRONG_SECTION CX_SY_RANGE_OUT_OF_BOUNDS STRING_OFFSET_NEGATIVE CX_SY_RANGE_OUT_OF_BOUNDS STRING_OFFSET_TOO_LARGE CX_SY_RANGE_OUT_OF_BOUNDS STRING_LENGT

28、H_NEGATIVE CX_SY_RANGE_OUT_OF_BOUNDS STRING_LENGTH_TOO_LARGE CX_SY_RANGE_OUT_OF_BOUNDS STRING_OFFSET_LENGTH_TOO_LARGE CX_SY_RANGE_OUT_OF_BOUNDS DATA_OFFSET_LENGTH_NOT_ALLOWED CX_SY_OFFSET_NOT_ALLOWEDSince a subfield access can occur in almost all statements, no keywords can be assigned.Exception gro

29、up: DYNAMIC_CALL_METHOD_ERRORS (Associated superclass: CX_SY_DYN_CALL_ERROR)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.DYN_CALL_METH_CLASS_ABSTRACT CX_SY_DYN_CALL_ILLEGAL_CLASS DYN_CALL_M

30、ETH_CLASS_NOT_FOUND CX_SY_DYN_CALL_ILLEGAL_CLASS DYN_CALL_METH_CLASSCONSTRUCTOR CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_CONSTRUCTOR CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_NOT_FOUND CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_NO_CLASS_METHOD CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_PRIVAT

31、E CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_PROTECTED CX_SY_DYN_CALL_ILLEGAL_METHOD DYN_CALL_METH_EXCP_NOT_FOUND CX_SY_DYN_CALL_EXCP_NOT_FOUND DYN_CALL_METH_PARAM_KIND CX_SY_DYN_CALL_ILLEGAL_TYPE DYN_CALL_METH_PARAM_LITL_MOVE CX_SY_DYN_CALL_ILLEGAL_TYPE DYN_CALL_METH_PARAM_TAB_TYPE CX_SY_DYN_CALL_

32、ILLEGAL_TYPE DYN_CALL_METH_PARAM_TYPE CX_SY_DYN_CALL_ILLEGAL_TYPE DYN_CALL_METH_PARAM_MISSING CX_SY_DYN_CALL_PARAM_MISSING DYN_CALL_METH_PARREF_INITIAL CX_SY_DYN_CALL_PARAM_MISSING DYN_CALL_METH_PARAM_NOT_FOUND CX_SY_DYN_CALL_PARAM_NOT_FOUND DYN_CALL_METH_REF_IS_INITIAL CX_SY_REF_IS_INITIALThe follo

33、wing keywords are assigned to this exception group:CALL METHODException group: FILE_ACCESS_ERRORS(Associated superclass: CX_SY_FILE_ACCESS_ERROR) 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 availabl

34、e to write or create the file, or if the authorization to access the file is missing.DATASET_CANT_CLOSE CX_SY_FILE_CLOSE DATASET_CANT_OPEN CX_SY_FILE_OPEN EXPORT_DATASET_CANNOT_OPEN CX_SY_FILE_OPEN DATASET_WRITE_ERROR CX_SY_FILE_IO DATASET_READ_ERROR CX_SY_FILE_IO EXPORT_DATASET_WRITE_ERROR CX_SY_FI

35、LE_IO DATASET_SEEK_ERROR CX_SY_FILE_POSITION DATASET_NO_POSITION CX_SY_FILE_POSITION DATASET_READ_ONLY CX_SY_FILE_OPEN_MODE DATASET_NOT_OPEN CX_SY_FILE_OPEN_MODE OPEN_DATASET_NO_AUTHORITY CX_SY_FILE_AUTHORITY OPEN_PIPE_NO_AUTHORITY CX_SY_FILE_AUTHORITY DATASET_TOO_MANY_FILES CX_SY_TOO_MANY_FILES DAT

36、ASET_NO_PIPE CX_SY_PIPES_NOT_SUPPORTEDException group: IMPORT_MISMATCH_ERRORS(Common class: CX_SY_IMPORT_MISMATCH_ERROR)This group contains runtime errors that may occur during the import of datafrom the ABAP memory,from the database,from the SHARED BUFFER, orfrom a fileif the type or the length of

37、the data stored is not identical with thatof the target type.CONNE_IMPORT_WRONG_COMP_DECS CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_COMP_LENG CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_COMP_TYPE CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_FIELD_DECS CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPO

38、RT_WRONG_FIELD_LENG CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_FIELD_TYPE CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_OBJECT_TYPE CX_SY_IMPORT_MISMATCH_ERROR CONNE_IMPORT_WRONG_STRUCTURE CX_SY_IMPORT_MISMATCH_ERRORIMPORT_ALIGNMENT_MISMATCH CX_SY_IMPORT_MISMATCH_ERRORIMPORT_WRONG_END_POS CX_SY

39、_IMPORT_MISMATCH_ERRORException group: LOCALIZATION_ERRORS(Common class: CX_SY_LOCALIZATION_ERROR)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 characte

40、r set which has not been released.TEXTENV_CODEPAGE_NOT_ALLOWED CX_SY_LOCALIZATION_ERROR TEXTENV_INVALID CX_SY_LOCALIZATION_ERRORTEXTENV_KEY_INVALID CX_SY_LOCALIZATION_ERRORException group: REMOTE_CALL_ERRORS(Associated superclass: CX_SY_REMOTE_CALL_ERROR)This group contains runtime errors that mayoc

41、cur during calls in remote systems (currently only CALLMETHOD).Typical examples are network errors or the unexpected termination of the connection.RMC_COMMUNICATION_FAILURE CX_SY_RMC_COMM_FAILURE RMC_INVALID_STATUS CX_SY_RMC_INVALID_STATUSRMC_SYSTEM_FAILURE CX_SY_RMC_SYSTEM_FAILURECALL METHODNot ass

42、igned to an exception group:ASSIGN_CASTING_ILLEGAL_CAST CX_SY_ASSIGN_CAST_ILLEGAL_CAST ASSIGN_CASTING_UNKNOWN_TYPE CX_SY_ASSIGN_CAST_UNKNOWN_TYPE ASSIGN_FIELD_NOT_IN_RANGE CX_SY_ASSIGN_OUT_OF_RANGE DATASET_OFFSET_TOO_LARGE CX_SY_FILE_POSITIONDYN_CALL_METH_NOT_IMPLEMENTED CX_SY_DYN_CALL_ILLEGAL_METHO

43、DEXPORT_BUFFER_NO_MEMORY CX_SY_EXPORT_BUFFER_NO_MEMORYGENERATE_SUBPOOL_DIR_FULL CX_SY_GENERATE_SUBPOOL_FULLMOVE_CAST_ERROR CX_SY_MOVE_CAST_ERRORPERFORM_PROGRAM_NAME_TOO_LONG CX_SY_PROGRAM_NOT_FOUNDREPLACE_INFINITE_LOOP CX_SY_REPLACE_INFINITE_LOOP6、类异常类树CX_SY_ROOT|-CX_STATIC_CHECK|-CX_DYNAMIC_CHECK|

44、| |-CX_SY_ARITHMETIC_ERROR| | | | |-CX_SY_ZERODIVIDE| | | | |-CX_SY_ARITHMETIC_OVERFLOW| | | | |-CX_SY_ARG_OUT_OF_DOMAIN| | | | |-CX_SY_PRECISION_LOSS| | |-CX_SY_ASSIGN_ERROR| | | | |-CX_SY_ASSIGN_CAST_ERROR| | | | | | |-CX_SY_ASSIGN_CAST_ILLEGAL_CAST| | | | | | |-CX_SY_ASSIGN_CAST_UNKNOWN_TYPE| | |

45、 | |-CX_SY_ASSIGN_OUT_OF_RANGE| | |-CX_SY_CODEPAGE_CONVERTER_INIT| | |-CX_SY_CONVERSION_ERROR| | | | |-CX_SY_CONVERSION_OVERFLOW| | | | |-CX_SY_CONVERSION_NO_NUMBER| | | | |-CX_SY_CONVERSION_CODEPAGE| | | | |-CX_SY_CONVERSION_BASE64| | | | |-CX_SY_CONV_ILLEGAL_DATE_TIME| | |-CX_SY_CREATE_ERROR| | | | |-CX_SY_CREATE_OBJECT_ERROR| | | | |-CX_SY_CREATE_DATA_ERROR| | |-CX_SY_DATA_ACCESS_ERROR| | | | |-CX_SY_RANGE_OUT_OF_BOUN

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论