如何在VC里面截获异常,就象delphi(300分)

  • 主题发起人 主题发起人 坏蟑螂
  • 开始时间 开始时间

坏蟑螂

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在VC里面截获异常,就象delphi的Try ...Exception ...end结构。<br>
 
怎么没人理我?<br>太EAsy吗?
 
也是 try 呀!<br><br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; "In try block, calling MyFunc()." &lt;&lt; endl;<br>&nbsp; &nbsp; &nbsp; &nbsp; MyFunc();<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; catch( CTest E )<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; "In catch handler." &lt;&lt; endl;<br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; "Caught CTest exception type: ";<br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; E.ShowReason() &lt;&lt; endl;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; catch( char *str )<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; cout &lt;&lt; "Caught some other exception: " &lt;&lt; str &lt;&lt; endl;<br>&nbsp; &nbsp; }<br>
 
__try { DoSomething1(); }<br>__except(EXCEPTION_EXECUTE_HANDLER) { DoSomething2(); }
 
随便下载个vc电子书都有呀?和delphi没两样
 
为什么不行?<br><br>void CMainFrame::OnEditWritelog()<br>{<br> char buffer[500];<br> CFile log_file;<br> CFileException* pFileError=NULL;<br><br> TRY<br> {<br> log_file.Open("LOGTHEM.TXT",CFile::modeCreate || CFile::modeReadWrite);<br> sprintf(buffer, "jfklsdjfkljsdkl/r/n");<br>// log_file.Seek(0,CFile::end);<br> log_file.Write(buffer, strlen(buffer));<br> log_file.Close();<br> }<br> CATCH(CFileException,pFileError)<br> {<br> switch (pFileError-&gt;m_cause)<br> {<br> case CFileException::generic:<br> MessageBox("generic");<br> break;<br> }<br> }<br> END_CATCH<br>}<br><br>
 
我找到问题所在了!<br>log_file.Open("LOGTHEM.TXT",CFile::modeCreate || CFile::modeReadWrite);<br>应该是<br>log_file.Open("LOGTHEM.TXT",CFile::modeCreate | CFile::modeReadWrite);<br><br>哈哈!好久不用C,忘完了。<br>得罪!得罪!<br>见笑!见笑!<br><br>
 
可是<br>log_file.Open("LOGTHEM.TXT",CFile::modeCreate || CFile::modeReadWrite);<br>为什么就不会产生异常呢?<br>
 
坏蟑螂 &nbsp;好像是一个游戏的名字?
 
因为CFile::modeCreate || CFile::modeReadWrite结果是数值<br>而CFile::modeCreate | CFile::modeReadWrite结果也是数值<br>所以对于。OPEN来说,参数无误<br>
 
多人接受答案了。
 
后退
顶部