procedure Tmediaplay.Image3Click(Sender: TObject);
var
 listitem:Tlistitem;
begin
 with listview2 do
 try
    begin
      listitem:=items.Add;
      listitem.Caption:=listview1.Selected.Caption;
     end;
 except
   on EAccessViolation do
      showmessage('没有歌曲可以转移,请添加歌曲!');
 end;
end;
我是想在Listview1中没有内容时能显示提示框中的内容,但总也不能实现,我是个初学者,对try……except……end不太会使用,请高手们指点。

解决方案 »

  1.   

    if listview1.Selected.Caption='' then 
    showmessage('没有歌曲可以转移,请添加歌曲!');
    try……except……end
    只有在引发异常时才发生,
    你的那段,就没什么异常
      

  2.   

    没有操作异常当然不会执行expect后的代码了。你可以在正常的程序里判断listview是否有内容
      

  3.   

    改成这样就可以引发异常了
    procedure Tmediaplay.Image3Click(Sender: TObject);
    var
     listitem:Tlistitem;
    begin
     with listview2 do
     try
        if trim(Listview1.Selectected.Caption) = '' then
          abort
        else
        begin
          listitem:=items.Add;
          listitem.Caption:=listview1.Selected.Caption;
         end;
     except
       on EAccessViolation do
          showmessage('没有歌曲可以转移,请添加歌曲!');
     end;
    end;
    我是想在Listview1中没有内容时能显示提示框中的内容,但总也不能实现,我是个初学者,对try……except……end不太会使用,请高手们指点。
      

  4.   

    哪位高手可以给个关于try……except方面的东东看看,让我学习一下,最好有关于exception的那些内容的东东。我的mail是:[email protected].谢谢各位!
      

  5.   

    所有的错误类型定义在 ...\borland\delphi6\source\trl\Sys\sysutils.pas
    凡是 E 打头的几乎全是。但这些类型不一定包括数据库返回的错误信息。try
    ...
    except
     on E: Exception do Showmessage(E.Message);
    end; //可以获得数据库返回的错误信息的内容
    --------------------------------------------------------
    try
     ...
    except
      on EZeroDivide do HandleZeroDivide;
      on EOverflow do HandleOverflow;
      on EMathError do HandleMathError;
    else
      HandleAllOthers;  //处理所有其它异常
    end;
    --------------------------------------------------------
    try
     ...
    except
      HandleException;  //默认处理
    end;
    try
    ...
    except
     on E: Exception do Showmessage(E.Classname);
    end;
    可以返回错误的类型,但一般数据库错误会笼统的显示 EDatabaseError
    --------------------------------------------------------
    你在调试程序的时候Delphi会在每个Exception处中断。
    运行你的*.exe你会发现一切如你所愿。如果你想在调试时不想系统弹出警告对话框,
    菜单 Tools\Debugger Options...\
    Language Exceptions\Stop on Delphi Exceptions 的钩钩去掉;
    Tools\Debugger Options...\
    OS Exceptions\Handled by 选user program;on resume选 Run Unhandled另外,如果你想要针对特定的异常进行处理,必须搞清楚它的准确名称。比如你的
    on EAccessViolation do,你确定引发的异常一定是EAccessViolation吗?如果不是,当然捕捉不到。
      

  6.   

    在调试时是没作用,直接运行exe就可以了