1
我有一個關於異常並將控制權返回給COM調用者的問題。下面是一個自定義異常類的測試問題。是什麼拋出新的CustomException與Marshal.ThrowExceptionForHR
Marshal.ThrowExceptionForHR(CustomException.COR_E_ARGUMENT);
和
throw new CustomException("Argument is out of bounds");
之間的區別我有點明白爲什麼1)和2,因爲它們返回一個int和一個例外對象不起作用。但是3和4有什麼區別?
public class CustomException : ApplicationException
{
public static int COR_E_ARGUMENT = unchecked((int)0x80070057);
public CustomException(string msg)
: base(msg)
{
HResult = COR_E_ARGUMENT;
}
}
你需要編寫將使用CustomException類 立即控制返回給調用者COM代碼段。您還需要確保 調用方有權訪問錯誤代碼。您使用哪個代碼段 ?
- return Marshal.GetExceptionForHR(CustomException.COR_E_ARGUMENT);
- return CustomException.COR_E_ARGUMENT;
- Marshal.ThrowExceptionForHR(CustomException.COR_E_ARGUMENT);
- 拋出新的CustomException(「Argument is out of bounds」); //正確答案