0

com オブジェクトの作成が (自分のクラス インスタンス コンストラクターで) 失敗した場合、どの例外をスローする必要がありますか? たとえば、Excel.Application オブジェクトを作成したいとします。失敗した場合は、Excel.Application コンストラクターによって生成された COMException で満たされた内部例外を使用して、特定の Exception をスローしたいと考えています。

4

1 に答える 1

1

独自の例外クラスを作成したい場合は作成できますが、新しい例外を内部例外にラップするためにそれを行う必要はありません。

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);
    }

}
于 2013-10-22T12:22:56.420 に答える