try{}catch 问题

qyaahoo 2015-01-06 04:55:28
下面代码获取html内容,但有时候不知道什么原因会报C++错误,怎么添加try{}catch 无论发生什么错误都不让他报错呢?
CString GetHtml(CHAR*  strFormData){
CString str;
CString info;
try{
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);

CHttpConnection* pConnection = session.GetHttpConnection( TEXT("www.test.cn"),//www.test.cn localhost
(INTERNET_PORT)80);
CHttpFile* pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
TEXT("/data.ashx"),
NULL,
1,
NULL,
TEXT("HTTP/1.1"),
INTERNET_FLAG_RELOAD);

//需要提交的数据
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
//下面这段编码,则是可以让服务器正常处理
//CHAR* strFormData = "username=0&password=0";
pFile->SendRequest( szHeaders,
szHeaders.GetLength(),
(LPVOID)strFormData,
strlen(strFormData));

DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);



try{
char szBuff[1025];
UINT nRead = 0;
while ((nRead = pFile->Read(szBuff,1024))>0)
{ info+=CString(szBuff,nRead);
}
}catch(char* value){}
session.Close();
pFile->Close();
delete pFile;
// AfxMessageBox(info);
return info;
}catch(char* value){}
return info;
}
...全文
226 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2015-01-09
  • 打赏
  • 举报
回复
try/catch只能处理throw的异常。如果不曾throw过, 当然处理不了: 比如硬件异常
hlx_beat 2015-01-09
  • 打赏
  • 举报
回复
try{} catch(...){ } 前提:C/C++/代码生成/启用C++异常/是,但有 SEH 异常 (/EHa)
lm_whales 2015-01-09
  • 打赏
  • 举报
回复
这是MFC异常 用 MFC方式捕捉处理,MFC 大约这么处理 MSDN 示例代码:
CFile* pFile = NULL;
// Constructing a CFile object with this override may throw
// a CFile exception and won't throw any other exceptions.
// Calling CString::Format() may throw a CMemoryException,
// so we have a catch block for such exceptions, too. Any
// other exception types this function throws will be
// routed to the calling function.
TRY
{
   pFile = new CFile(_T( "C:\\WINDOWS\\SYSTEM.INI"), 
      CFile::modeRead | CFile::shareDenyNone);
   ULONGLONG dwLength = pFile->GetLength();
   CString str;
   str.Format(_T("Your SYSTEM.INI file is %I64u bytes long.") , dwLength);
   AfxMessageBox(str);
}
CATCH(CFileException, pEx)
{
   // Simply show an error message to the user.
   pEx->ReportError();
}
AND_CATCH(CMemoryException, pEx)
{
   // We can't recover from this memory exception, so we'll
   // just terminate the app without any cleanup. Normally, 
   // an application should do everything it possibly can to
   // clean up properly and not call AfxAbort().
   AfxAbort();
}
END_CATCH
// If an exception occurs in the CFile constructor,
// the language will free the memory allocated by new
// and will not complete the assignment to pFile.
// Thus, our cleanup code needs to test for NULL.
if (pFile != NULL)
{
   pFile->Close();
   delete pFile;
}
encoderlee 版主 2015-01-09
  • 打赏
  • 举报
回复

CString GetHtml(CHAR* strFormData)
{
	CString str;
	CString info;
	CInternetSession session;
	CHttpConnection* pConnection = NULL;
	CHttpFile* pFile = NULL;
	try
	{
		session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
		session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
		session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
		pConnection = session.GetHttpConnection(TEXT("www.test.cn"), (INTERNET_PORT)80);
		pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
			TEXT("/data.ashx"),
			NULL,
			1,
			NULL,
			TEXT("HTTP/1.1"),
			INTERNET_FLAG_RELOAD);
		CString szHeaders   = L"Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
		pFile->SendRequest( szHeaders,
			szHeaders.GetLength(),
			(LPVOID)strFormData,
			strlen(strFormData));
		DWORD dwRet;
		pFile->QueryInfoStatusCode(dwRet);
		char szBuff[1025];  
		UINT nRead = 0;  
		while ((nRead = pFile->Read(szBuff,1024))>0)
		{   
			info+=CString(szBuff,nRead);
		}  
	}
	catch (CInternetException *pe)
	{
		pe->Delete();
		if (pFile)
		{
			pFile->Close();
			delete pFile;
		}
		if (pConnection)
		{
			pConnection->Close();
			delete pConnection;
		}
		session.Close();
	}
	return info;
}
CInternetSession、CHttpConnection、CHttpFile这组API会抛出CInternetException异常,需要处理CInternetException异常
姚自新 2015-01-09
  • 打赏
  • 举报
回复
捕获所有异常:try {} catch(...) {}
ztenv 版主 2015-01-07
  • 打赏
  • 举报
回复
c++中的try..catch不是万能的,尽量别用
赵4老师 2015-01-07
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
qyaahoo 2015-01-06
  • 打赏
  • 举报
回复
请求的这个文件data.ashx加个sleep系统就会立即崩溃 上面少了几个字
qyaahoo 2015-01-06
  • 打赏
  • 举报
回复
如果不加上面的try代码,请求的这个文件加个sleep系统就会立即崩溃 加了上面的try catch好多了,但还是会报错,但变得非常少了
qyaahoo 2015-01-06
  • 打赏
  • 举报
回复
应该是获取数据异常导致的某种错误。系统直接崩溃了 反复模块测试,发现就是这里的代码有问题但又不太清楚原因。所以强制不能让他报错
子曰过 2015-01-06
  • 打赏
  • 举报
回复
报什么错?访问了非法内存?

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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