Abnormal Program Termination Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 2
Reputation: HappyOrangutan is an unknown quantity at this point 
Solved Threads: 0
HappyOrangutan HappyOrangutan is offline Offline
Newbie Poster

Abnormal Program Termination Problem

 
0
  #1
Sep 29th, 2009
The output of the code below is:

start
Caught One! Ex. #: 1
Caught One! Ex. #: 2
Abnormal Program Termination

I don't understand why the exception isn't caught.


  1. #include <iostream>
  2. using namespace std;
  3. // Different types of exceptions can be caught.
  4. void Xhandler(int test)
  5. {
  6. try{
  7. if(test) throw test;
  8. else throw "Value is zero";
  9. }
  10. catch(int i) {
  11. cout << "Caught One! Ex. #: " << i << '\n';
  12. }
  13. catch(char *str) {
  14. cout << "Caught a string: ";
  15. cout << str << '\n';
  16. }
  17. }
  18. int main()
  19. {
  20. cout << "start\n";
  21. Xhandler(1);
  22. Xhandler(2);
  23. Xhandler(0);
  24. Xhandler(3);
  25. cout << "end";
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: Abnormal Program Termination Problem

 
0
  #2
Sep 30th, 2009
I'm not sure what your problem is, when I compile and run it:

start
Caught One! Ex. #: 1
Caught One! Ex. #: 2
Caught a string: Value is zero
Caught One! Ex. #: 3
end
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 341
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 50
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz

Re: Abnormal Program Termination Problem

 
0
  #3
Sep 30th, 2009
use a typecast in the throw tag
  1. throw (char *)"Value is zero";

it should work. I have tried in my machine by using it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Abnormal Program Termination Problem

 
0
  #4
Sep 30th, 2009
If at all you want to throw string literals, remember that they are const char* not simply char*. So refactor your code as:
  1. void Xhandler(int test)
  2. {
  3. try
  4. {
  5. if (test) throw test;
  6. else throw "Value is zero";
  7. }
  8. catch (int i)
  9. {
  10. cout << "Caught One! Ex. #: " << i << '\n';
  11. }
  12. catch (const char *str)
  13. {
  14. cout << "Caught a string: ";
  15. cout << str << '\n';
  16. }
  17. }
But then, before you find throwing string literals very comfortable, read this article http://www.informit.com/articles/art...30642&seqNum=5
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: HappyOrangutan is an unknown quantity at this point 
Solved Threads: 0
HappyOrangutan HappyOrangutan is offline Offline
Newbie Poster

Re: Abnormal Program Termination Problem

 
0
  #5
Sep 30th, 2009
Thank you siddhant3s, that solved the problem. I will give the article a good read.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC