I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions? -
#include <iostream>
-
-
using namespace std;
-
-
string msgZero = "Zero denominator\n";
-
-
void inverse(long value, double& answer) {
-
-
if (value > 0) {
-
-
answer = 1.0/value;
-
-
}
-
-
else {
-
-
if (value == 0) {
-
-
throw msgZero;
-
-
}
-
-
else {
-
-
throw value;
-
-
}
-
-
}
-
-
return; }
-
-
-
-
void fraction (long n, long d, double& result) {
-
-
inverse(d, result);
-
-
result = n * result;
-
-
return;
-
-
}
-
-
int main () {
-
-
while (true) {
-
-
long numer, denom;
-
-
double ans;
-
-
cout << "Enter numerator and
-
-
denominator: ";
-
-
if ((cin >> numer >> denom) == 0) break;
-
-
try {
-
-
fraction(numer, denom, ans);
-
-
}
-
-
catch (char* str) {
-
-
cout << str; }
-
-
cout << "Skipped the first catch block"<< endl;
-
-
catch (long val) {
-
-
cout << "Negative denominator "<<
-
-
val<< endl; }
-
-
cout << “*********”<< endl;
-
-
}
-
-
return (0);
-
-
}
-
-
3 2539
I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions? -
#include <iostream>
-
-
using namespace std;
-
-
string msgZero = "Zero denominator\n";
-
-
void inverse(long value, double& answer) {
-
-
if (value > 0) {
-
-
answer = 1.0/value;
-
-
}
-
-
else {
-
-
if (value == 0) {
-
-
throw msgZero;
-
-
}
-
-
else {
-
-
throw value;
-
-
}
-
-
}
-
-
return; }
-
-
-
-
void fraction (long n, long d, double& result) {
-
-
inverse(d, result);
-
-
result = n * result;
-
-
return;
-
-
}
-
-
int main () {
-
-
while (true) {
-
-
long numer, denom;
-
-
double ans;
-
-
cout << "Enter numerator and
-
-
denominator: ";
-
-
if ((cin >> numer >> denom) == 0) break;
-
-
try {
-
-
fraction(numer, denom, ans);
-
-
}
-
-
catch (char* str) {
-
-
cout << str; }
-
-
cout << "Skipped the first catch block"<< endl;
-
-
catch (long val) {
-
-
cout << "Negative denominator "<<
-
-
val<< endl; }
-
-
cout << “*********”<< endl;
-
-
}
-
-
return (0);
-
-
}
-
-
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
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 *
Is this a typo? I think you mean
String is not equivalent to char *
Yes....Thats a Typo.
Thanks for pointing it.
Raghuram
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
by: Arjen |
last post by:
Hi,
I'm doing this:
try {
try {
}
catch(Exception ex){
throw;
|
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...
|
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...
|
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...
|
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()
|
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...
|
by: dick |
last post by:
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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: 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...
|
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...
| |