473,396 Members | 1,933 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,396 software developers and data experts.

there are illogical results by using try and catch block

there are illogical results by using try and catch block

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Circle_computations
  5. {
  6.  
  7. public:
  8.     double area,circumference,radius,pi;
  9.  
  10. public:
  11.  
  12.  
  13.  Circle_computations()
  14. {
  15.  }    
  16.   Circle_computations(double radi)
  17. {
  18. pi = 3.14;
  19. radius=radi;
  20.  
  21.  
  22. }
  23.  
  24. double c_area()
  25. {
  26.  
  27. area = pi*radius*radius;
  28. return area;
  29.  
  30. }
  31.  
  32. double c_circumferunce()
  33. {
  34.  
  35. circumference =2*pi*radius;
  36. return circumference;
  37.  
  38. }
  39.  
  40. };
  41.  
  42. class Cylinder:public Circle_computations
  43. {
  44.  
  45. protected:
  46.     double height,volume,cyarea;
  47.  
  48. public:
  49. Cylinder(double r, double h) :
  50.     Circle_computations(r)
  51. {
  52.     height = h;
  53. }
  54.  
  55.  
  56.     double cy_area()
  57. {
  58.     cyarea=(2 * (Circle_computations::c_area()))+( c_circumferunce()* height) ;
  59.  
  60. cout<<cyarea;
  61. }
  62.  
  63.  
  64. double c_volume()
  65. {
  66.     volume = (Circle_computations::c_area())* height;
  67.  
  68.     return volume;
  69. cout<<volume;
  70. }
  71.  
  72.  
  73. };
  74.  
  75.  
  76. int main()
  77.  
  78. {
  79. double r,heigh_t;
  80. cout<<"**Please Enter The Radius First** \n";
  81. cin>> r;
  82. cout<<"**Please Enter The height** \n";
  83. cin>>heigh_t;
  84.  
  85. try
  86. {
  87. if(r||heigh_t<=0.0)
  88. {
  89. throw "PLESE ENTER double NUMBERS";
  90.  
  91.  
  92. }
  93. }
  94.  
  95. catch (char* strException)
  96. {
  97.     cerr << "Error: " << strException << "\n";
  98. }
  99.  
  100. Circle_computations  compute(r);
  101. Cylinder computecylider(r,heigh_t);
  102.  
  103. cout<<"****The cylinder surface  Area   is****\n";
  104. cout<<computecylider.cy_area();
  105.  
  106. cout<<"\n";
  107. cout<<"****The cylinder volume   is****\n";
  108. cout<<computecylider.c_volume();
  109.  
  110. cout<<"\n";
  111.  
  112.  
  113.  
  114. }
Oct 24 '13 #1
1 1030
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.   if(r||heigh_t<=0.0)
  4.   {
  5.     throw "PLESE ENTER double NUMBERS";
  6.   }
  7. }
  8. catch (char* strException)
  9. {
  10.   cerr << "Error: " << strException << "\n";
  11. }
Line 3, strange construct this will through an exception if r is any non-zero value or heigh_t is less than 0 did you mean if((r <- 0.0) || (heigh_t<=0.0)) i.e. throw if either variable is less than 0?

Line 5, do not throw a string, throw a std::exception or any sub-class of it defined in stdexcept.

Whole code, very strange indeed why are you using a exception for this when it is functionally equivilent to

Expand|Select|Wrap|Line Numbers
  1. if(r||heigh_t<=0.0)
  2. {
  3.   cerr << "Error: PLESE ENTER double NUMBERS\n";
  4. }
And finally don't use '\n' use std::endl, i.e.

Expand|Select|Wrap|Line Numbers
  1. if(r||heigh_t<=0.0)
  2. {
  3.   cerr << "Error: PLESE ENTER double NUMBERS" << std::endl;
  4. }
Oct 24 '13 #2

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

Similar topics

4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
3
by: Travis | last post by:
I am trying to prevent an exception from bubbling up from one nested Try/Catch Block to its "Parent" Try/Catch Block. Here is some example code: try{ try{ //Non-application killing code that...
18
by: Simon | last post by:
I was of the impression that code placed after a Try...Catch block was only executed if there was no exception thrown. I've got some VB.net code as part of a Windows form that executes even...
2
by: Solution Seeker | last post by:
Hi, I have a Problem when i try to put a Try Catch Block Outside all the Functions in a Class. But in .Net, it throws an error. I like to catch all the Unhandled Exception that occured inside...
1
by: Solution Seeker | last post by:
Hi, I have a Problem when i try to put a Try Catch Block Outside all the Functions in a Class. But in .Net, it throws an error. I like to catch all the Unhandled Exception that occured inside...
2
by: davidz | last post by:
When a catchable error occurs in a try block, control is passed to the associated catch block. While I am then able to examine properties of the error using such functions as ERROR_NUMBER() in the...
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...
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
8
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
Is it right when placing the RETURN statement inside the TRY or inside the CATCH statement, when there is a FINALLY clause? Especially when there is a transaction going on, in the try/catch block?...
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.