我想捕捉當我嘗試使用Neo4jClient.dll連接到我的neo4j數據庫時發生的異常。如果數據庫處於脫機狀態,則會出現以下錯誤消息:「在mscorlib.dll中發生類型'System.AggregateException'的異常,但未在用戶代碼中處理。」我的catch-block永遠不會到達。如何捕捉外部DLL中的異常?
這是我的代碼:
class Neo4JConnector
{
private static GraphClient client = null;
public Neo4JConnector(IniConfigSource configSource)
{
if (client == null)
{
client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));
try
{
client.Connect();
}
catch (Exception ex)
{
Console.WriteLine("Cannot connect"); // never reached :(
}
然後我試圖使用「外部」修改這個代碼:
class Neo4JConnector
{
private static GraphClient client = null;
[DllImport("Neo4jClient.dll", EntryPoint="Connect")]
static extern void Connect();
public Neo4JConnector(IniConfigSource configSource)
{
if (client == null)
{
client = new GraphClient(new Uri(configSource.Configs["Configuration"].Get("Neo4jUrl")));
try
{
Connect();
}
catch (Exception ex)
{
Console.WriteLine("Cannot connect");
}
}
但是我得到的卻是一個例外,說:「[系統。在Neo4jClient.dll
這是如何在Neo4jClient.dll
簽名看起來像一個名爲'連接'在DLL'Neo4jClient.dll'中的入口點。public virtual void Connect();
我的代碼有什麼問題?有沒有更好的方法來捕捉外部異常?請幫助:(
if其簽名說明at是'虛擬',那麼你可以覆蓋它,因爲你希望包括想要的連接失敗 – EpiGen
{{catch} {}你能提供給完整的異常堆棧跟蹤嗎? –