944,101 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 999
  • C++ RSS
Sep 29th, 2009
0

Abnormal Program Termination Problem

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HappyOrangutan is offline Offline
2 posts
since Sep 2009
Sep 30th, 2009
0

Re: Abnormal Program Termination Problem

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
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Sep 30th, 2009
0

Re: Abnormal Program Termination Problem

use a typecast in the throw tag
C++ Syntax (Toggle Plain Text)
  1. throw (char *)"Value is zero";

it should work. I have tried in my machine by using it.
Reputation Points: 121
Solved Threads: 61
Posting Pro in Training
dkalita is offline Offline
402 posts
since Sep 2009
Sep 30th, 2009
0

Re: Abnormal Program Termination Problem

If at all you want to throw string literals, remember that they are const char* not simply char*. So refactor your code as:
CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Sep 30th, 2009
0

Re: Abnormal Program Termination Problem

Thank you siddhant3s, that solved the problem. I will give the article a good read.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
HappyOrangutan is offline Offline
2 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: wanted solution
Next Thread in C++ Forum Timeline: Allegro, without graphics card





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC