nane 发表于 2015-9-20 08:12:54

浅析在C#里面抛出SAP里面自定义的异常信息

  首先运行“SE37”在Exceptions页面增加异常信息,Exception为异常信息的代码,Short Text则为异常信息的详细文本,如图:

  C#里面如果需要抛出用户自定义的异常,那么使用RfcAbapException即可,这个Exception是专门用来获取用户自定义的异常的。



1   public void GetAllInfo(RfcDestination prd)
2         {
3             RfcRepository repo = prd.Repository;
4             IRfcFunction irfc = repo.CreateFunction("ZAGETSAPDATAT");
5             try
6             {
7               irfc.SetValue("VTYPE", "0");
8               irfc.Invoke(prd);
9               string Value = irfc.GetValue("RRESULT").ToString();
10             }
11
12            //RfcAbapException   用于获取用户自定义的异常信息。
13             //RfcTypeConversionException用于获取数据之间类型转换失败的异常信息。
14             catch (RfcAbapException ex)
15             {
16               //Documentation获取对应的异常的说明文字.通过Key来获取。
17               MessageBox.Show(irfc.Metadata.GetAbapException(ex.Key).Documentation);
18             }
19
20
21             IRfcTable table = irfc.GetTable("IT_ZMYTB2");
22             DataTable dt = new DataTable();
23             dt.Columns.Add("USERID");
24             dt.Columns.Add("USERPWD");
25             dt.Columns.Add("USERADDRESS");
26
27             for (int i = 0; i < table.RowCount; i++)
28             {
29               table.CurrentIndex = i;
30               DataRow dr = dt.NewRow();
31
32               dr["USERID"] = table.GetString("USERID");
33               dr["USERPWD"] = table.GetString("USERPWD");
34               dr["USERADDRESS"] = table.GetString("USERADDRESS");
35               dt.Rows.Add(dr);
36             }
37
38             dgv.DataSource = dt;
39             //prd = null;
40             //repo = null;
41         }
  效果如下图所示,抛出的错误信息就是SAP里面自定义的Exception。
页: [1]
查看完整版本: 浅析在C#里面抛出SAP里面自定义的异常信息