Exception Handling

Posts   
 
    
Posts: 33
Joined: 31-May-2005
# Posted on: 07-Oct-2005 04:15:49   

OK, I feel like I'm missing something really obvious. Here's the issue ...

I am using an IEntityValidator derived class for validating both field and entity level variables. Right now I'm not using any of the field level Validators generated by LLBL. The problem is that I can't catch the exceptions.

Here's the Validation code:

    class ItemEntityValidator : IEntityValidator
    {
        bool IEntityValidator.Validate(object containingEntity)
        {
            ItemEntity entity = (ItemEntity)containingEntity;

            //Field level validation
            if (entity.Descr.Length < 5)
            {
                throw new ORMEntityValidationException("Description Length must be at least 5 characters long.", entity);
            }

            //If exception not thrown, it's valid
            return true;
        }
    }

And here's the code that saves the entity and generates the exception:

        public bool SaveItem(ItemEntity itemToSave)
        {
            Boolean retVal = false;

            //Validate before saving
            itemToSave.EntityValidatorToUse = new ItemEntityValidator();

            try
            {
                using (DataAccessAdapter adapter = new DataAccessAdapter(DBConnection.ConnectionString))
                {
                    retVal = adapter.SaveEntity(itemToSave, true, null, false);
                }
            }
            catch (ORMEntityValidationException ex)
            {
                Console.WriteLine("caught it");
            }

            return retVal;
        }

As expected, I can save invalid data and the Validator throws the ORMEntityValidationException. But the Manager class that calls SaveEntity isn't catching it (receive an Unhandled Exception for ORMEntityValidationException). What am I doing wrong?

Thanks in advance, Josh Lindenmuth

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 07-Oct-2005 09:51:22   

So the exception is of type ORMEntityValidationException, but it's ending up somewhere else?

Perhaps it's due to the using statement. If you formulate it like:


using (DataAccessAdapter adapter = new DataAccessAdapter(DBConnection.ConnectionString))
{
    try
    {
        retVal = adapter.SaveEntity(itemToSave, true, null, false);
    }
    catch (ORMEntityValidationException ex)
    {
        Console.WriteLine("caught it");
        throw;  // not necessary if you don't want it to bubble upwards.
    }
}

do you still get the same behavior?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 33
Joined: 31-May-2005
# Posted on: 07-Oct-2005 15:45:19   

It's not the using keyword, but I did figure out that it's only in debug mode. When I run it in release mode, all exceptions are handled without any issue. Have you ever seen this? Did I just setup something incorrectly? Other Exceptions seem to be handled fine in debug, which is why I'm rather confused.

Thanks, Josh

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 07-Oct-2005 16:41:27   

Odd stuff. All I can think of is that there's a type mismatch: some code uses a different ORMSUpportClasses dll than other code (but while typing this I think this is just not possible at runtime).

I haven't seen this myself .

Frans Bouma | Lead developer LLBLGen Pro
Posts: 33
Joined: 31-May-2005
# Posted on: 07-Oct-2005 22:21:03   

Well, I got it working. I'm using VS2005, which may have been part of the problem. When I recompiled LLBLGen with VS2005 (I had been using the 1.1 libraries), the problem persisted if I compiled it in Release mode. However, if I compiled in debug mode, there were no problems. Is this a strange VS2005 bug, or should I expect such behavior?

Thanks for the help, Josh Lindenmuth

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 07-Oct-2005 23:06:47   

JoshLindenmuth wrote:

Well, I got it working. I'm using VS2005, which may have been part of the problem. When I recompiled LLBLGen with VS2005 (I had been using the 1.1 libraries), the problem persisted if I compiled it in Release mode. However, if I compiled in debug mode, there were no problems. Is this a strange VS2005 bug, or should I expect such behavior?

Thanks for the help, Josh Lindenmuth

Could you try with the 1.0.2005.1 beta, it has native 2.0 build ormsupportclasses libs. Though exceptions should be caught no matter what I'd say...

Frans Bouma | Lead developer LLBLGen Pro
franek
User
Posts: 10
Joined: 15-Jun-2006
# Posted on: 29-Sep-2006 22:00:01   

I have similar problem - there is no way to catch validation exception from assemblies other than DAL. Problem doesn't seem to exist when application is running without debugging. The only way I found to make this work in debugger is disabling "enable Just My Code" in debugger options.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 30-Sep-2006 02:42:14   

Please post this in a new thread along with the version that you are running.