捕获所有异常并将它们作为特定类型重新抛出是一种很好的做法 - 基本上将从应用程序的特定部分(或服务)抛出的所有异常进行分类?捕获所有异常并将它们作为分类方面的特定类型异常重新抛出是否是一种好的做法?
喜欢的东西:
// 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
}
}
}
我不同意,因为你不知道他的结构如何实现。更多我经常需要这两个堆栈跟踪,因为有多种代码路径可以到达相同的地方 – 2010-10-22 09:00:52