English 中文(简体)
我如何妥善处理普通类中的错误?
原标题:How do I properly handle an error in a generic class?

我正在从事一个通用的班级工作,正在处理错误。 我正在某个地方使用<条码>海水<>条码>,我正在发现一个错误。 问题是,我如何把这一错误退回到电话方法?

    public static DataTable GetData(string connString, string sqlStatement, Action<iDB2ParameterCollection> parameters)
    {
        DataTable dt = new DataTable();

        using (iDB2Connection conn = new iDB2Connection(connString))
        {
            using (iDB2Command cmd = new iDB2Command(sqlStatement, conn))
            {
                conn.Open();
                if (parameters != null) { parameters(cmd.Parameters); }
                try
                {
                    using (iDB2DataAdapter da = new iDB2DataAdapter(cmd)) { da.Fill(dt); }
                }
                catch (iDB2SQLErrorException e)
                {
                }

                conn.Close();
            }
        }

        return dt;
    }
最佳回答

出于同样的原因,我们这样做了。 因此,你可以确保联系结束。

我们只是重新犯了同样的错误,失去“最终”集团的联系。 这使这一联系变得封闭,而且仍然无法与呼吁者保持联系,因为“最终”的路障不管怎样都得到执行。

catch (iDB2SQLErrorException e) 
{ 
   throw e;
} 
finally
{
   cn.Close();
}

The above code is what we ve used for years, but thanks to the comments, I think it might need tweaking. See this blog post for info on how to preserve the stack trace with exception handling: http://weblogs.asp.net/fmarguerie/archive/2008/01/02/rethrowing-exceptions-and-preserving-the-full-call-stack-trace.aspx

问题回答

不将其赶上基类!

在基类一级,我不是抓获和吞.例外的狂热。

让我们的课堂对这些细节感到担忧。

Side Note (Evidence of position): You ll notice that in practically any API, the doucmentation will report what exceptions will be thrown with classes. If they were to catch them in a base class, they have effectively swallowed them rendering you helpless as the user of said classes.

Additional Articles:

...instead of writing our abstractions based on details, the we should write the details based on abstractions.

这是依赖性转移原则的核心支柱。

http://www.oo Design.com/ Design-principles.html” rel=“nofollow”http://www.oo Design.com/ Design-principles.html。

  1. You could implement some logic to handle to exception internally in this method and re-throw it again. The exception will bubble up in the call stack;
  2. Other option is to use error codes to pass the error up in the stack. It depends on the API.

要么不追捕,让打电话者处理,要么丢掉你自己的错误,把原来的错误总结为:

class DataRetrievalException : Exception {
    DataRetrievalException(String message, Exception cause) : base(message, cause) {}
}

// ...
catch (iDB2SQLErrorException e) {
    throw new DataRetrievalException("Error retrieving data from database", e);
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签