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

an error handling code doubt

I want this code to call generic catch block whenever any special character(eg. !,@,#,$,%,&,*, etc) is entered in numerator or denominator. So what code should I add to this.
Any help would be appreciated. Thanks.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     try                                                // generic catch block
  8.     {  
  9.        int num, den;
  10.        double division;
  11.        try
  12.        {
  13.          cout << "\n enter numerator(+ve no. only): ";
  14.          cin >> num ;
  15.          if (num<0) throw 3.2 ;
  16.        }    
  17.  
  18.        catch(double m)
  19.        {
  20.           cout << "\n ERROR: numerator should be positive ";
  21.           num = -num ;
  22.           cout << "\n\n So your new numerator is: " << num;
  23.        }         
  24.  
  25.        try
  26.        {
  27.           cout << "\n\n enter denominator: ";
  28.           cin >> den ;
  29.           if (den==0) throw 1;
  30.        }
  31.  
  32.        catch(int n)
  33.        {
  34.           cout << "\n ERROR: You cannot divide a no. by zero, enter another no: ";
  35.           cin >> den;
  36.        }
  37.  
  38.        division = double(num)/den;
  39.        cout << "\n result is: " << division << "\n\n ";
  40.     }
  41.  
  42.     catch(...)                              // Generic catch block
  43.     {
  44.        cout << "/n/n Unexpected Error: Contact your software provider";
  45.     }  
  46.     system("PAUSE");
  47.     return EXIT_SUCCESS;
  48. }
  49.  
Feb 25 '12 #1
1 1398
weaknessforcats
9,208 Expert Mod 8TB
I suggest you write a function that tests you input for the undesrired values and, if found, does a throw of an exception object.
which to catch as an exception&. Try to use the catch(...) as a last resort. Unknown exception types flyung around a C++ program is not a good idea.

For example, if the input is less than zero, throw an out_of_range exception. Your what method() can explain what the probem is. Do this rather than catching a double.

Just derive your exception class from std::exception and implement the what() method.
Feb 25 '12 #2

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

Similar topics

8
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
1
by: Tom | last post by:
Suppose you have code structure like this: Option Explicit Dim MyVariable As Single Private Sub CallingProcedure() Call CalledProcedure() ....Do This ... End Sub Private Sub...
3
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
3
by: thomasp | last post by:
If I have a Try Catch block that captures an exception in a function located in a module called by a form, in other words burried in the code, and this exception is not one that can be corrected,...
1
by: scottrm | last post by:
I want to get the file name, line number etc. in error handling code in an asp.net application. I know how to trap errors in the Global.asax and I read that you can use the stack frame from the...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
3
by: jonamukundu | last post by:
Hi everyone out there I have a rather funny problem. Error handling code routines do not seem to work on my laptop. I still get the default error messages when i test. One my desktop the code...
2
by: nguyenminhhai | last post by:
Hi everyone, I'm reading "The C++ Programming Language" (Bjarne Stroustrup, 3rd edition). At page 193, he said "Doing error handling using the same level of abstraction as the code that caused the...
9
by: arnuld | last post by:
Can anyone point me to some online/offline document on error handling in C ? I am doing some Socket Programming in C and feeling a lots of difficult in error handling :( Also some time ago...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.