2017-11-23 17 views
0

내 응용 프로그램에 생성 된 예외를 처리하는 프로 시저가 있습니다. untThreadDSR에 예외가 생성되면 예외가 처리되어야하는 프로 시저에 의해 예외가 캡처되지 않습니다. 예외가 다른 양식에서 생성 된 경우 프로 시저가 올바르게 포착합니다.스레드에서 생성 된 예외를 잡아서 조작 할 수있는 방법은 무엇입니까

절차는 주요 데이터 모듈에 있습니다

... 
private 
    { Private declarations } 
    procedure HandleExceptions(Sender: TObject; E: Exception); 
... 

procedure DM.HandleExceptions(Sender: TObject; E: Exception); 
begin 
    if pos('UNIQUE KEY',E.Message) <> 0 then 
    begin 
    if pos('UNQ_CUSTOMER_NAME',E.Message) <> 0 then 
     MsgMsg('Customer already registered.',2) 
    else if pos('UNQ_USERS_LOGIN',E.Message) <> 0 then 
     MsgMsg('User already registered.',2); 
    end else 
    if pos('ERROR_DATASYS',E.Message) <> 0 then 
    MsgMsg('The Server Date is incorrect. Please contact your network administrator!',2) 
    else //Other messages will be sent to Support 
    SendReport(E.Message, V_COMPANY, V_LOGIN, V_APP_VERSION, Sender); 
end; 

untThreadDSR 다음과 같이

unit untThreadDSR; 

interface 

uses 
    Classes, SysUtils, Messages, Windows, Forms; 

type 
    TThreadDSR = class(TThread) 
    procedure DSR_Start; 
    private 
    { Private declarations } 
    protected 
    procedure Execute; override; 
    end; 

implementation 

uses untDM; 

procedure TThreadDSR.DSR_Start; 
begin 
    //I put this intentionally just to generate an exception 
    StrToInt(''); 
    //If this is on any other form, the exception is caught by "HandleExceptions", but from here on out! 
end; 

procedure TThreadDSR.Execute; 
begin 
    Synchronize(DSR_Start); 
end; 

end. 

TThreadDSR가 호출 :

procedure TFrmDSR.btnExecuteClick(Sender: TObject); 
var 
    Thread: TThreadDSR; 
begin 
    Thread := TThreadDSR.Create(true); 
    Thread.FreeOnTerminate := true; 
    Thread.Resume; 
end; 
+0

예외는 주 스레드에서 발생합니다. 너가 원하는게 그거야? –

+0

"TThreadDSR"에서 생성 된 모든 예외가 "DM.HandleExceptions"에 걸리므로 오류 메시지를 처리 ​​할 수 ​​있기를 바랍니다. –

답변

2

예외가되지 크로스 스레드 경계를 않습니다. 잡히지 않은 예외가 TThread.Synchronize()이라는 프로 시저를 이스케이프하면 Synchronize()은 예외를 캡처하여 작업자 스레드의 컨텍스트에서 다시 발생시킵니다. Execute()이 이후에 예외를 catch하지 않으면 스레드는 종료되고 TThread.FatalException 속성에 예외를 할당합니다.

TThread.OnTerminate 이벤트 (주 스레드의 컨텍스트에서 호출 됨)를 사용하여 FatalExceptionHandleExceptions() 프로 시저에 전달할 수 있습니다.

+0

대단히 감사합니다! –