作者suhue (豐風)
看板Programming
標題[問題] C++ try-catch語法
時間Mon Sep 24 13:40:58 2007
我想請問我寫的try-catch語法有沒有錯誤,
因為每次compile時,總是出現那行expression syntax的錯誤
可是找不出問題出在哪,我已經照課本上的方式寫了
能不能請大家幫我看一下
#include<iostream.h>
void main()
{ int n;
int a[5]={1,2,3,4,5};
cout<<"n="<<endl;
cin>>n;
try{ if(n<0) -------------------->expression syntax
throw "invalid number n";
int s=0;
for(int i=0;i<n;i++)
s=s+a[i];
cout<<"sum="<< s<<endl;
}
catch(char *e)
{
cout<<e<<endl;
};
};
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.31.174.119
1F:→ StubbornLin:首先要改的是你的縮排125.231.227.138 09/24 14:06
2F:→ suhue:請回答重點好嗎? 61.31.174.119 09/24 14:17
3F:→ suhue:我使用BCB,不知道工具是否有差? 61.31.174.119 09/24 14:18
4F:推 sunneo:完全無差220.132.228.138 09/24 15:15
6F:→ sunneo:那是一個clpstr,所以請catch(const char*e)220.132.228.138 09/24 15:25
7F:→ sunneo:說真的,縮排真的很糟220.132.228.138 09/24 16:09
8F:→ suhue:對不起,為什麼我複製你的程式再用BCB run 61.31.174.119 09/24 19:09
9F:→ suhue:結果還是錯誤一堆? 61.31.174.119 09/24 19:09
10F:→ suhue:我的縮排好像只有一個地方沒對齊而已吧!哪糟 61.31.174.119 09/24 19:10
11F:推 wowtiger:第一次看到縮排是這樣縮的XD 有創意 218.163.100.85 09/24 19:56
12F:→ sunneo:何不把所謂更多的錯誤訊息po出來看看?220.132.228.138 09/24 21:31
13F:→ sunneo:去C/CPP問看看吧...我轉過去220.132.228.138 09/24 21:36
※ sunneo:轉錄至看板 C_and_CPP 09/24 21:36
14F:→ sunneo:何不檢查專案是否正確?副檔名...220.132.228.138 09/24 21:49
15F:推 sunneo:只有一個地方沒對齊嗎?220.132.228.138 09/24 22:35
16F:→ sunneo:try的end scope, catch為什麼空一行?220.132.228.138 09/24 22:36
17F:→ sunneo:依照你的規則,應該是{後面空兩個空白開始220.132.228.138 09/24 22:37