Reputation: 893
I am good with C and know basics of C++ and g++ to execute my code. I am trying try catch exceptions in C++. My code inside try section is mostly C code. I wrote:
try{
....
}
catch (std::exception& e)
{
std::cerr << "exception caught: " << e.what() << '\n';
}
In the try section, I tried couple of bad code:
char str[3];
free(str); // Memory Violation
this gave me Segmentation fault and program terminated
while(1); // stack overflow
this kept on running for long. I did ^c to abort it
int x = 5/0; // divide by 0 exception
This gave me some random or zero value.
int arr[5];
cout<<arr[100] // array out of bound
this gave me some garbage value
cout<< arr[1000] /// memory violateion
This gave me seg fault
I don't want to use, throw statement. I want to protect my code, in case I throw any random error, my program should catch it, print it and then move on to next thing.
I read alot on website. I understand C++ doesn't handle segmentation fault as an exception.
My majority of code will be in C. IN some cases I will be using std: vector/ std: string I don't want to use throw,
Here I have couple of questions:
1) can't C++ throw, an exception without using throw statement or exception function. 2) For testing my code I am looking for example which cause, stack overflow, array out of bond , division by zero or any sort of exception. Can someone point to some examples
3) is try catch in C++ made works only for exception handling specific to only STD?
Later I also tried using set_terminate and set_unexpected:
using the following code:
typedef void (*pfv)();
void my_terminate()
{ cout << "Call to my terminate" << endl; }
void my_unexpected()
{ cout << "Call to my unexpected" << endl; }
int Divisor()
{
int result;
result = number%num;
if (result == 0 && num < 21)
{
num+1;
Divisor();
if (num == 20 && result == 0)
{
return number;
}
}
else if (result != 0)
{
number++;
Divisor();
}
}
int main ()
{
pfv old_term = set_terminate(my_terminate);
pfv old_unex = set_unexpected(my_unexpected);
Divisor();
}
This should have outputted result to call of my_unexpected via set_terminate
But I got segmentation fault.
I have used g++ to run my programs and all i get is segmentation fault as a result in case of exception.
Seems like I am missing some basic and important exception. need some inputs
Upvotes: 0
Views: 376
Reputation: 2320
No. You can actually turn off throw support in most compilers. Interoperability with C is one reason why you might want to do it; C has no support for throw at all.
Unlike Java or C#, there is no intermediate code interpreter that detects these conditions, so they will not throw exceptions when they occur. C and C++ are much closer to the hardware; at the processor level, there is no "throw" construct and no stack unwinding.
No. In C++, you can throw anything; integers, pointers, references, objects... you name it. You don't necessarily have to extend std::exception. Catching std::exception is useful when you make heavy use of standard libraries that might throw it, but there is no reason that you have to use it if you don't want to.
Upvotes: 1