0

i have the following program using exception handling.

#include<iostream.h> #include<conio.h> void divide(int x,int y,int z) { cout<<"\n Inside function 1 \n"; if((x-y)!=0) { int R=z/(x-y); cout << "Result =" << R<<"\n"; } // end of if else { throw(x-y); } // end of else } // end of void int main() { try { cout<< "Inside try block\n"; divide(10,20,30); divide(10,10,20); } //end of try catch(int i) { cout<< "Caught\n"; } return 0; } //end of main 

When compiling i get the following errors

Function throw should have a prototype.

Undefined symbol "try"

Statement missing ;

Function should return a value.

Please help me.Thanks a lot

6
  • 4
    What language are you compiling as? Compile as C++ (g++, not gcc. Or make the project settings in VS C++, not C). Commented Sep 5, 2010 at 4:08
  • 2
    Use standard headers. <conio.h> is non-standard. Commented Sep 5, 2010 at 4:09
  • Your code compiles and runs nicely with gcc after changing the include and import the std namespace. Commented Sep 5, 2010 at 6:22
  • Why are you throwing an exception here in the first place? Is this a homework question? Commented Sep 5, 2010 at 7:08
  • Would it surprise you to hear that if you compile C++ code with an ancient C compiler, you get errors? Commented Sep 5, 2010 at 10:22

4 Answers 4

2

Compile with some non-ancient C++ compiler, use #include <iostream> instead of #include <iostream.h>, and drop #include <conio.h>, then it should just work once you have using std::cout;

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you are missing

#include <exception> 

Also, typically you should throw an object of a derived class from std::exception

UPDATE: Like the comments point out, including exception is not required. It is required for using std::exception only.

The fix is to use a different compiler. From wikipedia, Turbo C is a C compiler. Thus, it won't support C++ exception handling.

4 Comments

The header isn't required, though.
I am using turbo c and if i include the header file <exception.h> i am getting the error as cannot include the header file #include < exception.h>
@S.PRATHIBA: None of the standard library headers have a .h extension. Read carefully. Also, that sounds like a C compiler to me...not a C++ compiler. There are many more modern and better compilers you should use, for free. See the c++ tag info.
To expand on GMan's first comment <exception> defines some classes (i.e. std::exception, std::bad_exception) types and functions. As the original code isn't using any of these, adding this include isn't going to help; I'm not sure why this has been voted up.
0

I think in turbo c if you point your cursor on anything and press F1 it gives the help. Try that with throw.

Comments

0

When you use turbo c , youll get this error. I dont know why, I tried different compiler like online gdb, it worked well there.

You cannot use iostream instead of iostream.h. Infact even string is not a data type according to turbo c

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.