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
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;
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.
I think in turbo c if you point your cursor on anything and press F1 it gives the help. Try that with throw.
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
精彩评论