Connecting Tech Pros Worldwide Forums | Help | Site Map

expection handing: error message "undefined symbol try . . "

Newbie
 
Join Date: Jan 2009
Posts: 4
#1: Jan 7 '09
how to work with expection handing in which when i am using try block then the error message coming that "undefined symbol try" why it is so, please solve my problem.

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Can you post the piece of code where u are getting the error?
Also which compiler u are using?

Raghu
Newbie
 
Join Date: Jan 2009
Posts: 4
#3: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Expand|Select|Wrap|Line Numbers
  1.  
  2. //Compile is turbo c++
  3.  
  4.  
  5. #include<iostream.h>
  6. #include<stdio.h>
  7. #include<conio.h>
  8. void main()
  9. {
  10. clrscr();
  11. int a,b;
  12. cout<<"Enter value of a and b\n";
  13. cin>>a>>b;
  14. int x=a-b;
  15. try
  16. {
  17. if(x!=0)
  18. {
  19. cout<<"Result(a/x)="<<a/x<<endl;
  20. }
  21. else
  22. {
  23. throw(x);
  24. }
  25. }
  26. catch(int i)
  27. {
  28. cout<<"Exption caughg:x="<<x<<endl;
  29. }
  30. cout<<"End";
  31. getch();
  32. }
  33.  
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Are you compiling as c or c++?
Is that the only error message you are getting?
Newbie
 
Join Date: Jan 2009
Posts: 4
#5: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


then what I should do and how can i run my programme
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#6: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


You should start by re-writing void main() as int main(). Main returns an int, always, anything else is undefined behaviour.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Quote:

Originally Posted by arvindkuk View Post

then what I should do and how can i run my programme

I don't think you started learning C++ by learning exceptions. How did you compile your hello world c++ program?
Newbie
 
Join Date: Jan 2009
Posts: 4
#8: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


my problem is "undefined symbol try" how can I handle this
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Your problem is not being sure which language you are coding in. You want to use try/catch exception handling so you must use c++ not c.
For c++ you need to change these things

1.) replace #include<iostream.h> with #include<iostream>
2.) Remove all other includes
3.) add using namespace std; before the definition of main
4.) Make main return an int not void. void is wrong even in c
5.) Remove lines with clrscr(); and getch(); in your code
6.) Compile the file as a c++ file not as a c source file.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,167
#10: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Something obvious that has not been mentioned yet is what is the name of your file?

if it is <something>.c then the compiler will almost certainly be compiling it as C code it needs to be <something>.cpp
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,366
#11: Jan 7 '09

re: expection handing: error message "undefined symbol try . . "


Also, keep in mind that exception handling is often turned off in most C++ compilers by default. You have have to enable it by setting a compiler switch.

That would explain your indefined symbol.
Reply