c/c++ try的用法
求大侠给出try的具体用法(最好给出样例)多谢了!
其实主要是为测该程序是否爆栈来的:
- C/C++ code
#include<cstdio>//8个方向遍历图#include<cstring>#include<cstdlib>#define N 640000#define M 800#define MM 5120000using namespace std;typedef struct{ int x,y;}Q;char g[M][M];Q queue[MM];int rear,head,t,w,h;const int neg=-1,pos=1;void dfs(int x,int y){ if(g[x][y]=='.'){ t++; g[x][y]='*'; queue[++rear].x=x; queue[rear].y=y; while(head<rear){ x=queue[++head].x; y=queue[head].y; if(pos+y<w)dfs(x,pos+y);//right if(neg+y>-1)dfs(x,neg+y);//left if(neg+x>-1){//upward dfs(neg+x,y); if(neg+y>-1)dfs(neg+x,neg+y); if(pos+y<h)dfs(neg+x,pos+y); } if(pos+x<h){//downward dfs(pos+x,y); if(neg+y>-1)dfs(pos+x,neg+y); if(pos+y<w)dfs(pos+x,pos+y); } } }}int main(){ int i,j,max; while(scanf("%d %d",&w,&h)==2){ fflush(stdin); for(i=0;i<h;i++)gets(g[i]); rear=head=-1; for(t=max=i=0;i<h;i++) for(j=0;j<w;j++) if(g[i][j]=='.'){ dfs(i,j); if(t>max)max=t; t=0; } printf("%d\n",max); } return 0;}[解决办法]
try
{
m_rec=theApp.m_conn->Execute((_bstr_t)sql,NULL,adCmdText);
while(!m_rec->adoEOF)
{
m_level.AddString((char*)(_bstr_t)m_rec->GetCollect("level"));
m_rec->MoveNext();
}
m_level.SetCurSel(0);
}
catch(_com_error &e)
{
MessageBox(e.Description());
return FALSE;
}
[解决办法]
void fun()
{
if(正常)
{
做你想做的
}
else if(爆了)
{
抛出异常
throw 1;
}
else if(空了)
{
抛出异常
throw 2;
}
}
try
{
fun();
}
catch (int ex)
{
if(ex == 1)
printf("爆了");
else if(ex == 2)
printf("空了");
}
catch (...)
{
printf("other error");
}
[解决办法]
这程序写的。。这可读性。。
没看出来使用try和catch的必要。