设计 DataLayer 或任何其他分层架构时如何处理异常

发布于 2024-09-08 18:37:11 字数 1270 浏览 4 评论 0原文

我正在创建一个数据访问层,我想在其中处理应该由业务层捕获的异常,并清楚地了解异常的来源。 我正在做这样的事情..

编辑

private void OpenConnection()
        {
                if (ConnectionState.Closed == _connection.State)
                    _connection.Open();
        }

在上面给出的代码中我知道异常的原因。并想把它扔给BL处理以显示消息。

但我在 DL 中仅将这个函数与其他一些代码行一起使用。

protected DataTable GetDataTable(string Query)
 {
     DataTable dt =new DataTable();
     SqlCommand cmd = InitializeCommand(Query);

     SqlDataAdapter adp = new SqlDataAdapter(cmd);
     try
     {
         OpenConnection();
         adp.Fill(dt);
         CloseConnection(true);
         return dt;
     }
     catch (SqlException ex)
     { throw ex; }
     finally
     {
         adp.Dispose();
         cmd.Dispose();
     }
 }

现在,如果在尝试打开连接时 OpenConnection() 中发生异常,那么当我期望它返回异常时,应用程序会在 OpenConnection 本身的 throw ex 行处崩溃。 我该如何处理这个问题。 此外,在第二个函数 GetDataTable 中扩展了我的问题,如果发生异常,应该抛出哪种异常,并具有与此时发生的相同的详细信息以及如何抛出。 我只知道这种方式,但我认为这是一种错误的方式。

throw new ApplicationException(ex.message,ex.innerexception)

编辑

假设与服务器的连接丢失或者我使用了错误的连接字符串。现在我使用 BL 的 GetDataTable 函数。哪里会出现异常,应该在哪里处理? 我想知道DL中出现的问题。考虑到BL不知道DL的代码

I am creating a data access layer where I want to handle exceptions that should be catched by Business layer with clear idea of the source of exception.
I am doing something like this..

EDIT

private void OpenConnection()
        {
                if (ConnectionState.Closed == _connection.State)
                    _connection.Open();
        }

In the above given code i know an reason for exception. And want to throw it to be handled at BL to display message.

But I am using this function in DL only with some other line of codes.

protected DataTable GetDataTable(string Query)
 {
     DataTable dt =new DataTable();
     SqlCommand cmd = InitializeCommand(Query);

     SqlDataAdapter adp = new SqlDataAdapter(cmd);
     try
     {
         OpenConnection();
         adp.Fill(dt);
         CloseConnection(true);
         return dt;
     }
     catch (SqlException ex)
     { throw ex; }
     finally
     {
         adp.Dispose();
         cmd.Dispose();
     }
 }

Now if an exception occurs in OpenConnection() while trying to open the connection then the application crashes at the line throw ex in OpenConnection itself while I was expecting it to return exception.
How should I handle this issue.
Moreover extending my question in the second function GetDataTable if an exception occurs what kind of exception should be thrown with same details as occured at this point and how.
I only know this way but this is a wong way i think.

throw new ApplicationException(ex.message,ex.innerexception)

Edit

Suppose Connection to server losts or I am using wrong connection string. Now I use GetDataTable function from BL. Where will exception occur and where should I handle it ?
I want to know the problem that occured in DL. Considering BL dont know the code of DL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-09-15 18:37:11

您可以做几件事。

您可以使用不同的 try 块,并根据出现问题的位置抛出新的异常:

 try
 {
     OpenConnection();
     adp.Fill(dt);
 }
 catch (SqlException ex)
 { throw new SqlException("Could not open/populate", ex); }
 finally
 {
     adp.Dispose();
     cmd.Dispose();
 }

 try
 {
     CloseConnection(true);
     return dt;
 }
 catch (SqlException ex)
 { throw new SqlException("Could not close connection", ex); }
 finally
 {
     adp.Dispose();
     cmd.Dispose();
 }

There are several things you can do.

You can have different try blocks, and throw new exceptions according to where you had the problem:

 try
 {
     OpenConnection();
     adp.Fill(dt);
 }
 catch (SqlException ex)
 { throw new SqlException("Could not open/populate", ex); }
 finally
 {
     adp.Dispose();
     cmd.Dispose();
 }

 try
 {
     CloseConnection(true);
     return dt;
 }
 catch (SqlException ex)
 { throw new SqlException("Could not close connection", ex); }
 finally
 {
     adp.Dispose();
     cmd.Dispose();
 }
沩ん囻菔务 2024-09-15 18:37:11

我的建议是删除捕获并重新抛出,因为这会破坏异常的堆栈跟踪。你可以尝试不带任何捕获,因为无论如何你都会抛出异常。通过这种方式,您可以保留堆栈跟踪并仍然使用finally:

protected DataTable GetDataTable(string Query)
 {
     DataTable dt =new DataTable();
     SqlCommand cmd = InitializeCommand(Query);

     SqlDataAdapter adp = new SqlDataAdapter(cmd);
     try
     {
         OpenConnection();
         adp.Fill(dt);
         CloseConnection(true);
         return dt;
     }
     finally
     {
         adp.Dispose();
         cmd.Dispose();
     }
 }

My suggestion would be to remove the catch and rethrow since this will destroy the stack trace of the exception. You can have a try with no catch since you are throwing an exception anyway. In this way you can preserve the stack trace and still use the finally:

protected DataTable GetDataTable(string Query)
 {
     DataTable dt =new DataTable();
     SqlCommand cmd = InitializeCommand(Query);

     SqlDataAdapter adp = new SqlDataAdapter(cmd);
     try
     {
         OpenConnection();
         adp.Fill(dt);
         CloseConnection(true);
         return dt;
     }
     finally
     {
         adp.Dispose();
         cmd.Dispose();
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文