C#_自定义Exception

版权声明:本文为Martin原创文章,未经Martin允许不得转载。 https://blog.csdn.net/qq_36279445/article/details/81257722

自定义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);

猜你喜欢

转载自blog.csdn.net/qq_36279445/article/details/81257722