簡體   English   中英

當數據集為null時拋出什么異常

[英]What exception to throw when dataset is null

我正在研究C#(ASP.NET)。 我正在從數據庫中檢索記錄並將結果存儲在DataSet中。 如果我的檢索dataSet為null或為空,我想拋出異常。 我應該拋出什么異常?

謝謝您的幫助。

你可以像這樣拋出你自己的Exception

 public class DatabaseConnectionException : ApplicationException
    {
        public DatabaseConnectionException () { }

        public DatabaseConnectionException (string message)
            : base(message) { }

        public DatabaseConnectionException (string message, Exception innerException)
            : base(message, innerException) { }
    }

通常,您可以拋出ArgumentNullException

if(dataSet == null)
   throw new ArgumentNullException("The datasource cannot be null");

最好創建自定義業務異常並提供有意義的錯誤消息

public class BusinessException:ApplicationException
{
//
}

if(dataSet == null)
   throw new BusinessException("The datasource cannot be null");

您可以創建自定義異常,例如

var dataSet = //your dataSet;

if(dataSet == null)
   throw new MyException

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM