18/08/22 12:27:18.26 DYb1gEuJ.net
Win32API関数でのエラー処理について質問です
var DllFilePath = "hogehoge.dll";
handle = LoadLibrary(DllFilePath);
if (handle == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}
↑のコードで例外をthrowするときに例外のMessageにDllFilePathの値を追加したいのですが
throw new System.ComponentModel.Win32Exception(DllFilePath)
とすると本来のエラーメッセージが上書きされてしまいます。
var DllFilePath = "hogehoge.dll";
try
{
handle = LoadLibrary(DllFilePath);
if (handle == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}
}
catch(System.ComponentModel.Win32Exception e)
{
throw new System.ComponentModel.Win32Exception($"{e.Message} \"{DllFileName}\"", e);
}
こうすれば期待通りの内容にできるのですがAPI呼び出しの度に例外処理を書くのは
コードが冗長になってしまうし何より面倒なのでうまいこと簡単に書けないか悩んでいます。
何かいい方法は無いですかね?