gpt4 book ai didi

design-patterns - 捕获所有异常并将它们作为特定类型的异常在分类方面重新抛出是一种好的做法吗?

转载 作者:行者123 更新时间:2023-12-04 07:08:40 27 4
gpt4 key购买 nike

<分区>

捕获所有异常并将它们作为特定类型重新抛出是否是一种好做法 - 基本上是对从应用程序的特定部分(或服务)抛出的所有异常进行分类?

类似于:

//  This class is used to label all the exceptions occured on 
// Race Cars Service calls as RaceCarsServiceException
public class RaceCarsServiceException : Exception
{

public class RaceCarsServiceException (Exception e)
: base("Something went wrong!", e)
{
// Basically I assign the exception e as the inner exception
//by using the constructor method of Exception class
}

}

public class RaceCarsService
{

// Some member fields, methods etc. here

public double GetMaxSpeed(CarModel model)
{
try
{
return (double)_carsWebService.GetMaxSpeed(model.ToString());
}

catch(Exception e)
{
throw new RaceCarsServiceException(e); // So all exceptions are rethrown
//as type of RaceCarsServiceException
}
}
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com