0
点赞
收藏
分享

微信扫一扫

C#_自定义Exception


 

自定义Exception,并且能捕捉到。

网上搜的估计都是一样的,不如自己写一个,需求是返回错误信息和返回一个自定义的List

 

定义 xxxxException.cs

using System;
using System.Collections.Generic;

public class DeliveryProgressException : Exception
{
public DeliveryProgressException(){ }
public DeliveryProgressException(string message, Exception inner) : base(message, inner) { }
public DeliveryProgressException(string message, List<string> deliverOrderIds) : base(message) {
this.DeliverOrderIds = deliverOrderIds;
}

public override string Message
{
get
{
return base.Message;
}
}
public List<string> DeliverOrderIds
{ get; set;}
}

DeliveryProgressException myException = new DeliveryProgressException("EXCEPTION",exceptionDeliverOrderIds);

throw myException;

new 一个Exception

 

捕捉到

try
{
result = psService.GetTopPackPallet(userName);
}
catch (DeliveryProgressException e)
{
return Ok(new { success = false, exceptionOrders = e.DeliverOrderIds, progressCode = e.Message});
}
return Ok(result);

 

举报

相关推荐

0 条评论