Contract.EnsuresOnThrow 方法

定義

根據所提供的例外狀況和條件,針對封入方法或屬性指定後置條件合約。Specifies a postcondition contract for the enclosing method or property based on the provided exception and condition.

多載

EnsuresOnThrow<TException>(Boolean)

根據所提供的例外狀況和條件,針對封入方法或屬性指定後置條件合約。Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.

EnsuresOnThrow<TException>(Boolean, String)

針對封入方法或屬性來指定後置條件合約,和當條件是 false 時,根據所提供之例外狀況和條件來顯示的訊息。Specifies a postcondition contract and a message to display if the condition is false for the enclosing method or property, based on the provided exception and condition.

EnsuresOnThrow<TException>(Boolean)

根據所提供的例外狀況和條件,針對封入方法或屬性指定後置條件合約。Specifies a postcondition contract for the enclosing method or property, based on the provided exception and condition.

public:
generic <typename TException>
 where TException : Exception static void EnsuresOnThrow(bool condition);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void EnsuresOnThrow<TException> (bool condition) where TException : Exception;
static member EnsuresOnThrow : bool -> unit (requires 'Exception :> Exception)
Public Shared Sub EnsuresOnThrow(Of TException As Exception) (condition As Boolean)

類型參數

TException

例外狀況類型,其會叫用後置條件檢查。The type of exception that invokes the postcondition check.

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test.

屬性

備註

condition 參數指定當封入方法或屬性異常終止,且擲回 TException 類型的例外狀況時,預期會 true 的後置條件。The condition parameter specifies a postcondition that is expected to be true when the enclosing method or property terminates abnormally and an exception of type TException is thrown.

  • 這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 您必須使用二進位重寫器來執行這個後置條件的執行時間強制。You must use the binary rewriter for run-time enforcement of this postcondition.

EnsuresOnThrow<TException>(Boolean, String)

針對封入方法或屬性來指定後置條件合約,和當條件是 false 時,根據所提供之例外狀況和條件來顯示的訊息。Specifies a postcondition contract and a message to display if the condition is false for the enclosing method or property, based on the provided exception and condition.

public:
generic <typename TException>
 where TException : Exception static void EnsuresOnThrow(bool condition, System::String ^ userMessage);
[System.Diagnostics.Conditional("CONTRACTS_FULL")]
public static void EnsuresOnThrow<TException> (bool condition, string userMessage) where TException : Exception;
static member EnsuresOnThrow : bool * string -> unit (requires 'Exception :> Exception)
Public Shared Sub EnsuresOnThrow(Of TException As Exception) (condition As Boolean, userMessage As String)

類型參數

TException

例外狀況類型,其會叫用後置條件檢查。The type of exception that invokes the postcondition check.

參數

condition
Boolean

要測試的條件運算式。The conditional expression to test.

userMessage
String

當運算式是 false 時所顯示的訊息。The message to display if the expression is false.

屬性

備註

condition 參數指定當封入方法或屬性異常終止,且擲回 TException 類型的例外狀況時,預期會 true 的後置條件。The condition parameter specifies a postcondition that is expected to be true when the enclosing method or property terminates abnormally and an exception of type TException is thrown. message 參數會指定 false條件時所要顯示的訊息。The message parameter specifies a message to display if the condition is false.

  • 這個方法呼叫必須位於方法或屬性的開頭,才會在任何其他程式碼之前。This method call must be at the beginning of a method or property, before any other code.

  • 此合約會向用戶端公開;因此,它必須只參考至少與封入方法相同的成員。This contract is exposed to clients; therefore, it must only reference members that are at least as visible as the enclosing method.

  • 您必須使用二進位重寫器來執行這個後置條件的執行時間強制。You must use the binary rewriter for run-time enforcement of this postcondition.

  • 如果 userMessage 不是常數位串常值,則工具可能無法理解合約。If userMessage is not a constant string literal, the contract may not be understood by tools.

適用於