Exceptions

Exceptions

Old forum URL: forums.lhotka.net/forums/t/3464.aspx


juddaman posted on Monday, September 03, 2007

Hi

I want to throw an ArgumentException if the id passed to my GetX factory is not valid. The best (most proper) way I can think to this is:

public static Day GetDay(Guid id)
        {
            if (!Day.Exists(id))
            {
                throw new ArgumentException("Invalid id.", "id");
            }
            return DataPortal.Fetch<Day>(new Criteria(id));
        }

However it involves to calls to the DB, so to increase performance it seams I could just throw a custom exception in the dataportal when no results are returned and catch this in the factory and rethrow as an argument exception. Like this:

public static Day GetDay(Guid id)
        {
            try
            {
                return DataPortal.Fetch<Day>(new Criteria(id));
            }
            catch (DataPortalException ex)
            {
                if (ex.BusinessException is NoResultException)
                {
                    throw new ArgumentException("Invalid id.", "id", ex);
                }
                else
                {
                    throw;
                }
            }
        }


private void DataPortal_Fetch(Criteria criteria)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cm = db.GetStoredProcCommand(FetchDaySP);
            db.AddInParameter(cm, DayIdParam, DbType.Guid, criteria.DayId);

            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(cm)))
            {
                bool isResult = dr.Read();
                if (!isResult)
                {
                    throw new NoResultException("No results.");
                }
                _DayId = criteria.DayId;
                _Date = dr.GetDateTime(DateCol);
            }           
        }

Does anybody see any problems with this? I presume the stack trace will be preserved in the inner exception in ArgumentException. But I may be missing something, I've just starting to get my head around error handling concepts using exceptions. Thanks to a previous thread I've got my logging plan sorted now I just want to focus the exceptions coming from my BOs.

Regards, George.

Marjon1 replied on Monday, September 03, 2007

George,

That is the method that we have used and while we haven't gone into the inner workings as to how our exceptions will work in the long run. It is working for us currently raise them this way, we also have security exceptions being thrown as part of the factory method (if a user doesn't have rights to create a new object).


juddaman replied on Tuesday, September 04, 2007

Hi Marjon,

Thanks for posting. I presumue you're talking about the second method I outlined?

Thanks

George

Marjon1 replied on Tuesday, September 04, 2007

George,

Yeah, it was the second method that I was referring to.

Regards,

Marjon

juddaman replied on Tuesday, September 04, 2007

Thanks Marjon.

George.

Copyright (c) Marimer LLC