views:

184

answers:

3

Hi,

Could anyone tell me why a problem in the noun model would not be caught by this try catch?

I have tried this on two different controller methods now, and both times, even if the linq2sql doesn't allow the data to be saved, the code never jumps into the catch block.

I've watched the noun object in the middle of the trace, and the isvalid property is false, but the modelstate isvalid is true. Either way, the code never jumps into the catch block.

I'm pulling my hair out about this. I feel like it will be something really silly.

The code all works similar to nerd dinner.

NounRepository nounRepository = new NounRepository();
        Noun noun = new Noun();
        try
        {                
            UpdateModel(noun);
            nounRepository.Add(noun);
            nounRepository.save();
        }
        catch (Exception ex)
        {
            ModelState.AddRuleViolations(noun.GetRuleViolations());
            return View(noun);
        }
    return View(noun);

Update

I have just added this code, and now the rules are coming back to the front end fine, so it just seems that the try catch isn't catching!

UpdateModel(noun);

            if (!noun.IsValid)
            {

                var errors = noun.GetRuleViolations();
                ModelState.AddRuleViolations(noun.GetRuleViolations());
                return View(noun);

            }

            nounRepository.Add(noun);
            nounRepository.save();

I'd rather not have to add code in this manner though, as it seems like an unnecessary duplication.

A: 

Are you doing something in another thread? That is often a cause exceptions not being caught.

Daniel Auger
nope, nothing threading going on. It's really odd, I can post more code if it would help, but I just can't understand why it when nounRepository.save() is called, it doesn't error out. I watch the code step through all the validation code, and then it just gets to the end of the try block.
optician
+1  A: 

You faced logical change in mvc - validation here do not throw exceptions. Indeed, you need to check it using if statement.

I doubt that exception is happening - you need to catch linq2sql exception anyway, code is correct. Also there is high a chance that inside 'save' or 'add' you have another catch - this is quite common mistake

Sergey Osypchuk
Thank you for the answer, the problem was another try catch I had used at an earlier point in the project, before I had this validation. Very important lesson learnt about try catch, thank you!
optician
+1  A: 

Programming Rule #1: catch ain't broken (AKA: SELECT ain't broken).

If you're really in doubt, open up the Debug menu, choose "Exceptions", then tick the box for "Common Language Runtime Exceptions" under "Thrown." This will cause the debugger to break on all first-chance exceptions. If the debugger doesn't break during your update, then the exception is never getting thrown in the first place.

Don't forget to untick when you're done, as the behaviour gets pretty annoying under normal usage.

P.S. Never catch System.Exception. Catch the specific type(s) of exception(s) that you know might actually be thrown.

Aaronaught
In every rule there is an exception.For example, finally is not executed for ThreadAbort exception :) And catch maybe too... do not remember exactly
Sergey Osypchuk
Thanks for your answer, turning on the exception handling would have solved this problem much quicker for me. An very good thing to know for the future. Thanks!
optician
@Sergey: That is exactly why you shouldn't try to catch `System.Exception`. It probably won't do what you want on an `OutOfMemoryException` either. ;)
Aaronaught