Monthly Archives: January 2008

Exception handling in LINQ to SQL

I’ve been doing stuff with LINQ to SQL for a while now and there’s one thing that keeps bugging me: Proper exception handling. IMHO LINQ to SQL should encapsulate the database. I mean, I don’t care which database (SQL Server, Oracle, whatever) I’m working with, but if I call SubmitChanges on the DataContext, I’ll get a SqlException if it fails on the database. I know that currently LINQ to SQL only supports SQL Server, but the model is just as valid for any other (relational)database. To my surprise there is no LinqException or something that encapsulates the SqlException, so now I have to write code that is aware of the fact that I’m dealing with SQL. You can of course solve this by using the Exception Handling Application Block, so you don’t deal with specific exceptions in code, but it still feels funky.