2013-03-04 2 views
4

내 프로젝트에서 System.Data.SQLite를 사용합니다. 출력 폴더에 System.Data.SQLite dll이 없을 때 FileNotFoundException을 catch 할 수 없습니다 (다른 예외는 괜찮습니다). 다음은 코드 exapmle입니다.System.Data.SQLite에 대한 FileNotFoundExceptions이 catch되지 않았습니다.

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      SQLiteConnection conn = new SQLiteConnection(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

MessageBox는 표시하지 않습니다. 내가 별도의 기능이 코드를 추출하고 예외 잘 작동 및 메시지 박스를 잡는 것보다 시도 캐치에서이 함수 호출을 래핑하는 경우 보여 주었다 :

private void DeclareConnection() 
    { 
     SQLiteConnection conn = new SQLiteConnection(); 
    } 

    private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      DeclareConnection(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

문제가 무엇입니까?

답변

0

참조 어셈블리를 찾을 수 없다는 사실로 인해 생성 된 예외를 catch 할 수 없습니다.

리플렉션을 사용하여 수동으로 어셈블리를로드하는 경우에만 예외를 catch 할 수 있습니다.

sqlite 어셈블리가 있는지 확인하려면 File.Exists()을 수행하십시오.

3

당신은 AppDomain.AssemblyResolve 이벤트를 처리해야합니다,

는 AssemblyResolve 이벤트 여기

AppDomain.CurrentDomain.AssemblyResolve += HandleAssemblyResolve; 

구독은 C#에서 86/64의 SQLite는 어셈블리의로드를 처리하는 몇 가지 예제 코드입니다

public static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args) 
    { 
     if (args.Name.Contains("System.Data.SQLite")) 
     { 
      if (_assembliesResolved) 
       return null; 

      Assembly returnValue; 

      string executingAssemblyPath = Assembly.GetExecutingAssembly().Location; 
      executingAssemblyPath = Path.GetDirectoryName(executingAssemblyPath); 

      if (Environment.Is64BitProcess) 
       executingAssemblyPath = Path.Combine(executingAssemblyPath, @"lib-sqlite\x64\", "System.Data.SQLite.dll"); 
      else //32 bit process 
       executingAssemblyPath = Path.Combine(executingAssemblyPath, @"lib-sqlite\x86\", "System.Data.SQLite.dll"); 

      returnValue = Assembly.LoadFrom(executingAssemblyPath); 

      _assembliesResolved = true; 

      return returnValue; 
     } 

     return null; 
    } 
0

첫 번째 경우에는 jit가 메소드를 치는 즉시 예외를 throw하기 때문에 예외를 catch 할 수 없습니다. 두 번째 경우에는 메소드가 jits되고 DeclareConnection 메소드를 jit하려고 할 때 예외가 발생합니다.

관련 문제