// KDException 异常处理 + ILog 日志记录示例 // 适用插件:Form/Operation/Validator/Report/Task // 依赖 API:KDException, ILog, LogManager // 相关检查:推荐避免使用空 catch 和 printStackTrace,日志推荐带上下文 using System; using System.Collections.Generic; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; public class ExceptionSample { private static readonly ILog Logger = LogManager.GetLogger(typeof(ExceptionSample)); public void ProcessOrder(DynamicObject order) { try { string billNo = order["billno"] as string; Logger.Info(string.Format("开始处理订单: {0}", billNo)); ValidateOrder(order); SaveOrder(order); Logger.Info(string.Format("订单处理完成: {0}", billNo)); } catch (KDException ex) { // 业务异常,记录日志并抛出 Logger.Error(string.Format("处理订单失败: {0}", ex.Message), ex); throw; } catch (Exception ex) { // 系统异常,包装为业务异常 Logger.Error(string.Format("系统异常: {0}", ex.Message), ex); throw new KDException("SYS001", "系统异常,请联系管理员", ex); } } public void ValidateOrder(DynamicObject order) { List errors = new List(); if (string.IsNullOrEmpty(order["BillNo"] /* PropertyName */ as string)) { errors.Add("单据编号不能为空"); } decimal amount = Convert.ToDecimal(order["FAmount"] ?? 0); if (amount <= 0) { errors.Add("金额必须大于零"); } if (errors.Count > 0) { throw new KDException("VALIDATE001", string.Join("; ", errors)); } } private void SaveOrder(DynamicObject order) { // TODO: 实际保存逻辑 } }