捕獲所有異常並將它們作爲特定類型重新拋出是一種很好的做法 - 基本上將從應用程序的特定部分(或服務)拋出的所有異常進行分類?捕獲所有異常並將它們作爲分類方面的特定類型異常重新拋出是否是一種好的做法?
喜歡的東西:
// 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