各层TRY EXCEPT之间的关系.

W9757 2013-05-08 01:28:27
DLL内部有一函数,用Try Except 拦截异常并用Messagebox()提示,在调用此函数时也用了Try Except拦截异常并用showmessage()提示.
在调试DLL时,函数内部的Try有拦截到异常并提示,但在编译成DLL后,再调用此DLL,函数内部的Try messagebox()异常没有显示.调用此函数处的try showmessage()有提示.想请教如何函数内部的messagebox()显示出来.
...全文
102 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sololie 2013-05-08
  • 打赏
  • 举报
回复
用断言+TRY结合来用
feiba7288 2013-05-08
  • 打赏
  • 举报
回复
引用 7 楼 W9757 的回复:
MessageBox(handle,'打开浏览数据完成.','提示',64); except on e: exception do//将实际的报错显示出来看下 MessageBox(handle,PChar('数据出错.' +#13#10+ e.message),'错误(部品)B',16); Exit; end; 结果和上面的一样.
把那个错误贴出来看下
W9757 2013-05-08
  • 打赏
  • 举报
回复
MessageBox(handle,'打开浏览数据完成.','提示',64); except on e: exception do//将实际的报错显示出来看下 MessageBox(handle,PChar('数据出错.' +#13#10+ e.message),'错误(部品)B',16); Exit; end; 结果和上面的一样.
W9757 2013-05-08
  • 打赏
  • 举报
回复
引用 3 楼 feiba7288 的回复:
第一份代码的86行的finally改为except except//finally if line<>'' then begin messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64) end; end;
改成EXCEPT后,有异常时什么提示都没有了,直接跳到第二部份36行的MessageBox(handle,'打开浏览数据完成.','提示',64);处,导入多少就显示多少.
feiba7288 2013-05-08
  • 打赏
  • 举报
回复
还要注意一点,dll的参数不要用string,改为PChar procedure tstockjc_6.ExcelToSql(filename:string); //部品导入 procedure Tstockjc_6.N5Click(Sender: TObject); var dialog:TOpenDialog; sql,filename:string; begin try with ADOQuery1 do begin close; sql.Clear; sql.Add('delete bp_pd'); ExecSQL; end; dialog:=TOpenDialog.Create(Self); dialog.FileName:='*.xls'; dialog.Filter:='*.xls'; if not dialog.Execute then Exit; filename:=dialog.FileName; ExcelToSql(filename); with ADOQuery1 do begin close; sql.Clear; sql.Add('update bp_pd set zsl=cksl'); ExecSQL; end; with ADOQuery1 do begin close; sql.Clear; sql.Add('select * from bp_pd order by bppf'); Open; end; MessageBox(handle,'打开浏览数据完成.','提示',64); except on e: exception do//将实际的报错显示出来看下 MessageBox(handle,PChar('数据出错.' +#13#10+ e.message),'错误(部品)B',16); Exit; end;
feiba7288 2013-05-08
  • 打赏
  • 举报
回复
sorry,看错了

    try
     i:=2;
     //j:=excelApplication1.Rows.Count;
     j:=excelWorksheet1.UsedRange[65536].Rows.Count+1;
 
     if j<2 then
     begin
       ShowMessage('表格数据为空!');
       Exit;
     end;
 
     //try将这个try移到上面试下。。。。。。。。。。。。。。。。。。。。。。。
      //  try
         while i<=j do
         begin
             if Trim(excelWorksheet1.Cells.Item[i,2])<>'' then
             begin
 
               if not AddtoSQL(excelWorksheet1.Cells.Item[i,1],   //<span style="color: #FF0000;">这个地方会报错,因为数据格式不下确.</span>
                               Trim(excelWorksheet1.Cells.Item[i,2]),
                               trim(excelWorksheet1.Cells.Item[i,3]),
                               Trim(excelWorksheet1.Cells.Item[i,4]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,5]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,6]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,7]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,8]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,9]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,10]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,11]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,12]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,13]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,14]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,15]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,16])) then
               begin
                  if line='' then
                  begin
                    line:=Inttostr(i);
                  end else
                  begin
                    line:=','+Inttostr(i);
                  end;
                  err_cod1:='数据格式出错在第:'+inttostr(i)+'行.'; 
                  exit;
               end;
             end;
             Inc(i);
         end;
       except //on E:Exception do
         MessageBox(stockjc_6.Handle,PChar('数据格式出错在第:'+inttostr(i)+'行.'),'错误(部品)A',64); //<span style="color: #FF0000;">就是它没有show出来</span>.
         Exit;
       end;
 
     finally
 
      if line<>'' then
      begin
       messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64)
      end;
     end;
feiba7288 2013-05-08
  • 打赏
  • 举报
回复
第一份代码的86行的finally改为except except//finally if line<>'' then begin messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64) end; end;
W9757 2013-05-08
  • 打赏
  • 举报
回复

procedure tstockjc_6.ExcelToSql(filename:string); //部品导入
var
  excelApplication1:TExcelApplication;
  excelWorksheet1:TExcelWorksheet;
  excelWorkbook1:TExcelWorkbook;
  i,j:Integer;
  line:string;
begin
     err_cod1:='';
     line:='';
     try
       excelApplication1:= TExcelApplication.Create(Application);
       excelWorksheet1:=TExcelWorksheet.Create(Application);
       excelWorkbook1:=TExcelWorkbook.Create(Application);
     except
       Application.MessageBox('没有安装excel!','错误',MB_ICONERROR+mb_Ok) ;
       Exit;
     end;

     try
       excelWorkbook1.ConnectTo(excelApplication1.Workbooks.Open(filename,EmptyParam,EmptyParam,
                                                                 EmptyParam,EmptyParam,EmptyParam,
                                                                 EmptyParam,EmptyParam,EmptyParam,
                                                                 EmptyParam,EmptyParam,EmptyParam,
                                                                 EmptyParam,EmptyParam,EmptyParam,0));
       excelWorkbook1.ConnectTo(excelApplication1.Workbooks[1]);
       excelWorksheet1.ConnectTo(excelWorkbook1.Worksheets[1] as _worksheet);

     except
        MessageBox(stockjc_6.Handle,'打开Excel出错.','错误',64);
        Exit;
     end;

     i:=2;
     //j:=excelApplication1.Rows.Count;
     j:=excelWorksheet1.UsedRange[65536].Rows.Count+1;

     if j<2 then
     begin
       ShowMessage('表格数据为空!');
       Exit;
     end;

     try
      //  try
         while i<=j do
         begin
             if Trim(excelWorksheet1.Cells.Item[i,2])<>'' then
             begin

               if not AddtoSQL(excelWorksheet1.Cells.Item[i,1],   //这个地方会报错,因为数据格式不下确.
                               Trim(excelWorksheet1.Cells.Item[i,2]),
                               trim(excelWorksheet1.Cells.Item[i,3]),
                               Trim(excelWorksheet1.Cells.Item[i,4]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,5]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,6]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,7]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,8]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,9]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,10]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,11]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,12]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,13]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,14]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,15]),
                               StrToFloat(excelWorksheet1.Cells.Item[i,16])) then
               begin
                  if line='' then
                  begin
                    line:=Inttostr(i);
                  end else
                  begin
                    line:=','+Inttostr(i);
                  end;
                  err_cod1:='数据格式出错在第:'+inttostr(i)+'行.'; 
                  exit;
               end;
             end;
             Inc(i);
         end;
       except //on E:Exception do
         MessageBox(stockjc_6.Handle,PChar('数据格式出错在第:'+inttostr(i)+'行.'),'错误(部品)A',64); //就是它没有show出来.
         Exit;
       end;

     finally

      if line<>'' then
      begin
       messagebox(stockjc_6.handle,PChar('由于表格式有误以下行未导入:'+line),'错误(部品)A',64)
      end;
     end;

     try
       excelApplication1.Disconnect;
       excelApplication1.Quit;
       excelApplication1.Free;
       excelWorksheet1.Free;
       excelWorkbook1.Free;
     except
       ShowMessage('关闭文件出错!');
     end;
//调用函数.

procedure Tstockjc_6.N5Click(Sender: TObject);
var
    dialog:TOpenDialog;
    sql,filename:string;
begin
 try
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('delete bp_pd');
      ExecSQL;
    end;

     dialog:=TOpenDialog.Create(Self);
     dialog.FileName:='*.xls';
     dialog.Filter:='*.xls';
     if not dialog.Execute then Exit;
     filename:=dialog.FileName;
     ExcelToSql(filename);

    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('update  bp_pd set zsl=cksl');
      ExecSQL;
    end;
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('select * from bp_pd order by bppf');
      Open;
    end;
    MessageBox(handle,'打开浏览数据完成.','提示',64);
    except
    MessageBox(handle,PChar('数据出错.'),'错误(部品)B',16); //这里就有显示.
    Exit;
    end;
feiba7288 2013-05-08
  • 打赏
  • 举报
回复
上代码。。。。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧