Set_terminate

The program call myterminate and std::terminate..
why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>       
#include <exception>      

void myterminate () {
  std::cerr << "termninate called\n";
  throw 0;     
}

int main (void) {
  std::set_terminate(myterminate);
  try 
  {
    throw 'x';
  }
  catch (int) { std::cerr << "caught int\n"; }
  system("pause");
}
Last edited on
You are throwing a char and and there is only a catch for an int.
This works. http://ideone.com/PdHHuo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>       
#include <exception>      

void myterminate () {
  std::cerr << "termninate called\n";
  throw 0;     
}

int main (void) {
  std::set_terminate(myterminate);
  try 
  {
    throw 'x';
  }
  catch (char) { std::cerr << "caught char\n"; }
}


EDIT: Link
Last edited on
http://www.cplusplus.com/reference/exception/set_terminate/
The function shall terminate execution of the program without returning to the caller.
Notice that if f is a function that does not implement the proper functionality it causes undefined behavior.
Ah ok, while unexpected returns to the caller .. ok thank you so much now I understand =)
Topic archived. No new replies allowed.