473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Try/Catch/Throw in C++

17
I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string msgZero = "Zero denominator\n";
  6.  
  7. void inverse(long value, double& answer) {
  8.  
  9.   if (value > 0) {
  10.  
  11.     answer = 1.0/value;
  12.  
  13.   }
  14.  
  15.   else {
  16.  
  17.     if (value == 0) {
  18.  
  19.         throw msgZero;
  20.  
  21.     }
  22.  
  23.     else {
  24.  
  25.       throw value;
  26.  
  27.     } 
  28.  
  29.   }  
  30.  
  31.   return; }
  32.  
  33.  
  34.  
  35. void fraction (long n, long d, double& result) {
  36.  
  37.    inverse(d, result);
  38.  
  39.    result = n * result; 
  40.  
  41.    return;
  42.  
  43. }     
  44.  
  45. int main () {
  46.  
  47.   while (true) {
  48.  
  49.     long numer, denom;
  50.  
  51.     double ans;
  52.  
  53.     cout << "Enter numerator and 
  54.  
  55.             denominator: ";
  56.  
  57.     if ((cin >> numer >> denom) == 0) break;
  58.  
  59.     try {
  60.  
  61.       fraction(numer, denom, ans);
  62.  
  63.     }
  64.  
  65.     catch (char* str) {
  66.  
  67.       cout << str; }
  68.  
  69.     cout << "Skipped the first catch block"<< endl;
  70.  
  71.     catch (long val) {    
  72.  
  73.       cout << "Negative denominator "<<
  74.  
  75.                val<< endl; }
  76.  
  77.     cout << “*********”<< endl;
  78.  
  79.   }     
  80.  
  81.   return (0);
  82.  
  83. }  
  84.  
  85.  
Feb 18 '08 #1
3 2539
gpraghuram
1,275 Expert 1GB
I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. string msgZero = "Zero denominator\n";
  6.  
  7. void inverse(long value, double& answer) {
  8.  
  9.   if (value > 0) {
  10.  
  11.     answer = 1.0/value;
  12.  
  13.   }
  14.  
  15.   else {
  16.  
  17.     if (value == 0) {
  18.  
  19.         throw msgZero;
  20.  
  21.     }
  22.  
  23.     else {
  24.  
  25.       throw value;
  26.  
  27.     } 
  28.  
  29.   }  
  30.  
  31.   return; }
  32.  
  33.  
  34.  
  35. void fraction (long n, long d, double& result) {
  36.  
  37.    inverse(d, result);
  38.  
  39.    result = n * result; 
  40.  
  41.    return;
  42.  
  43. }     
  44.  
  45. int main () {
  46.  
  47.   while (true) {
  48.  
  49.     long numer, denom;
  50.  
  51.     double ans;
  52.  
  53.     cout << "Enter numerator and 
  54.  
  55.             denominator: ";
  56.  
  57.     if ((cin >> numer >> denom) == 0) break;
  58.  
  59.     try {
  60.  
  61.       fraction(numer, denom, ans);
  62.  
  63.     }
  64.  
  65.     catch (char* str) {
  66.  
  67.       cout << str; }
  68.  
  69.     cout << "Skipped the first catch block"<< endl;
  70.  
  71.     catch (long val) {    
  72.  
  73.       cout << "Negative denominator "<<
  74.  
  75.                val<< endl; }
  76.  
  77.     cout << “*********”<< endl;
  78.  
  79.   }     
  80.  
  81.   return (0);
  82.  
  83. }  
  84.  
  85.  

Issue 1)You are throwing a string and in the catch block catching char *.
String is equivalent to char*
To find this add catch(...)after the last catch block and you can catch the exception there

Raghuram
Feb 18 '08 #2
Banfa
9,065 Expert Mod 8TB
Issue 1)String is equivalent to char*
Is this a typo? I think you mean

String is not equivalent to char *
Feb 18 '08 #3
gpraghuram
1,275 Expert 1GB
Is this a typo? I think you mean

String is not equivalent to char *

Yes....Thats a Typo.

Thanks for pointing it.



Raghuram
Feb 18 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Arjen | last post by:
Hi, I'm doing this: try { try { } catch(Exception ex){ throw;
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
3
by: will | last post by:
Hi all. I've got an question about how to catch an exception. In Page_Load, I place a DataGrid, dg1, into edit mode. This will call the method called GenericGridEvent. GenericGridEvent will call...
26
by: Grim Reaper | last post by:
Just a quick and probably daft question... Isn't the code; Try DoSomething() Catch e As Exception HandleError(e) Finally DoThisAtTheEnd()
9
by: Mr Flibble | last post by:
Hi all, happy Friday! (certainly Friday is a day worth celebrating). I have a question on try/catch design (an exciting Friday topic for sure): I can either put a try/catch block in every...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
4
by: Fred | last post by:
Is it possible to use throw in javascript without try..catch? As far as I know, you must call it from within a try..catch block, or the function that calls throw must itself be called from within...
4
by: Elmo Watson | last post by:
I've got one main method, with a Try Catch block. In that, I catch the error and put it on a label. However, inside the Try portion, I also run another method. I have a Try/Catch block there,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.