簡體   English   中英

創建com對象時構造函數中的異常

[英]Exception in constructor while creating com object

如果創建com對象失敗(在我自己的類實例構造函數中),我應該拋出什么異常? 例如,我要創建Excel.Application對象。 如果失敗,我想拋出特定的Exception,並用Excel.Application構造函數生成的COMException填充內部異常。

如果要創建自己的異常類,則可以,但是不必這樣做就可以將新異常包裝在內部異常周圍。

public class CustomException : Exception
{
}

public static void main()
{

    try
    {
       //Code that instantiates COM object.

    }
    catch(Exception ex)
    {
       throw new CustomException("This is my message.  I can put anything I want to, then pass the real exception as the inner exception", ex);
    }

}

暫無
暫無

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

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