Catching exceptions in the UI

Catching exceptions in the UI

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


albruan posted on Tuesday, September 19, 2006

I have a couple of questions regarding returning errors to the UI.  I instantiated and populated five new Drawing objects, deliberately setting one of them to use the same DeptID and DrawingCode as a Drawing object that had previously been persisted to the database, and attempted to save the objects.  As expected, an exception was thrown by SQL Server since I have a unique index set on the Drawings table based on the DeptID and DrawingCode.  If I place the call to the Save method in a Try...Catch block and catch the DataPortalException, I get a "Cannot insert duplicate key row in object 'dbo.Drawings' with unique index 'DrawingCode'." message in the UI. 

How do I go about informing the user which entry caused the exception to be thrown?  Is there a way to catch the error prior to calling the Save method?  I thought about possibly using a variation of the Contains method and will look into it this evening as I have two appointments to keep today which will take up the balance of my day. 

ajj3085 replied on Tuesday, September 19, 2006

I think you're stuck examining the message; unfortunately there's not just enough detail in SqlException to help you determine which object caused the error.

skagen00 replied on Tuesday, September 19, 2006

In my base class I catch SqlException, test the code, and throw a custom exception (such as ConcurrencyException).

So if you want to give a more useful exception to the UI layer, wrap data access, catch exceptions, and rethrow as either custom exceptions (if they match certain categories you want to throw) or rethrow as the original exception.

 

Copyright (c) Marimer LLC