473,386 Members | 2,042 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.

bool method() throw in conditional statement

Hi

if I have a method like this

bool myClass::myMothod(myType& mt, herType ht) throw ( SomeExp );

how can I use it in a conditional statement

if (
try {
myMothod(myType& mt, herType ht)
} catch (const someExp& e) {
/* do something */
}
)
{
/* the body of the if ... */
}

it is a bit over my head.

thanks

Dec 22 '06 #1
5 2515
Gary Wessle wrote:
Hi

if I have a method like this

bool myClass::myMothod(myType& mt, herType ht) throw ( SomeExp );

how can I use it in a conditional statement

if (
try {
myMothod(myType& mt, herType ht)
} catch (const someExp& e) {
/* do something */
}
)
{
/* the body of the if ... */
}
The try clause must enclose one or more complete statements, so it
cannot be placed within the if clause itself. Generally the try clause
in this situation would enclose (at least) the entire if expression -
including an else cause (if one is present):

try
{
if (myMethod( mt, ht ))
{
...
}
else
{
...
}
}
catch (someType& e)
{
// handle or rethrow
}

Note that the try clause can have a larger scope (including a scope in
the function that called the current one or any of its callers). In
fact, exceptions are most useful in situations where some distance
separates the point in the program at which the exception was thrown
and the point in the program that the exception will be handled.

Greg

Dec 22 '06 #2
Gary Wessle wrote:
Hi

if I have a method like this

bool myClass::myMothod(myType& mt, herType ht) throw ( SomeExp );

how can I use it in a conditional statement

if (
try {
myMothod(myType& mt, herType ht)
} catch (const someExp& e) {
/* do something */
}
)
{
/* the body of the if ... */
}

it is a bit over my head.

thanks
You can't. You can, however do one of the following:
/* Option 1*/
try
{
if(myMethod(mt,ht))
{
/* the body of the if ... */
}
}
catch(const someExp &e)
{
/* do something */
}

/* Option 2*/
bool test = false;
try
{
test = myMethod(mt,ht);
}
catch(const someExp &e)
{
/* do something */
}

if(test)
{
/* the body of the if ... */
}

--
Clark S. Cox III
cl*******@gmail.com
Dec 22 '06 #3
"Greg" <gr****@pacbell.netwrites:
Gary Wessle wrote:
Hi

if I have a method like this

bool myClass::myMothod(myType& mt, herType ht) throw ( SomeExp );

how can I use it in a conditional statement

if (
try {
myMothod(myType& mt, herType ht)
} catch (const someExp& e) {
/* do something */
}
)
{
/* the body of the if ... */
}

The try clause must enclose one or more complete statements, so it
cannot be placed within the if clause itself. Generally the try clause
in this situation would enclose (at least) the entire if expression -
including an else cause (if one is present):

try
{
if (myMethod( mt, ht ))
{
...
}
else
{
...
}
}
catch (someType& e)
{
// handle or rethrow
}

Note that the try clause can have a larger scope (including a scope in
the function that called the current one or any of its callers). In
fact, exceptions are most useful in situations where some distance
separates the point in the program at which the exception was thrown
and the point in the program that the exception will be handled.

Greg
does this include when a method calls another one in a composition
situation?

that is method A::a calls method B::b which Calls method C::c that
throws, can I catch and handle inside A::a? if yes, this is
amazing. can you give an example please?
thanks

Dec 22 '06 #4

Gary Wessle wrote in message ...
>
does this include when a method calls another one in a composition
situation?
that is method A::a calls method B::b which Calls method C::c that
throws, can I catch and handle inside A::a? if yes, this is
amazing. can you give an example please?
thanks
ONLY because you said "please". <G>

// ------------------------------------
#include <iostream // #include <ostream>
#include <stdexcept>
#include <vector>

class Example{
public: // ------------------------------ public
void execute( std::ostream &cout ){
// ------------
cout<<"\n--- Exception test Bumfaddle ---"<<std::endl;

try{
BumFaddle Bf;
Bf.funcy( cout );
std::vector<intVint(2);
Vint.at(3); // out_of_range
} // try
catch( const std::out_of_range &Oor ){
cout<<"out_of_range caught: "<<Oor.what()<<std::endl;
}
catch( const std::exception &e ){ // Salt's example
cout<< "exception error: "<< e.what() <<std::endl;
}
// if you put "catch( out_of_range )" here, it will never get
// to it due to the 'higher-up' above.
catch( ... ){ // catch anything not caught above.
cout<<"caught something (maybe the flu!!)"<<std::endl;
}
cout<<"\n--- Exception test Bumfaddle ---end"<<std::endl;
// ------------
} // execute(ostream&)
// ------------------------------------
private: // ------------------------------ private
// ------------------------------------
class BumFaddle{ public:
void funcy( std::ostream &out){
try{ throw std::runtime_error("from funcy");}
catch( std::runtime_error &Re){
out<<"BumFaddle caught: "<<Re.what()<<std::endl;
throw; // send it on
}
} // funcy(ostream&)
}; // class BumFaddle
// ------------------------------------
}; // class Example
// ------------------------------------

int main(){
{
Example Ex;
Ex.execute( std::cout );
}
return 0;
} // main()
// ------------------------------------

Is that what you are after?

--
Bob R
POVrookie
Dec 23 '06 #5
Gary Wessle wrote:
"Greg" <gr****@pacbell.netwrites:
Note that the try clause can have a larger scope (including a scope in
the function that called the current one or any of its callers). In
fact, exceptions are most useful in situations where some distance
separates the point in the program at which the exception was thrown
and the point in the program that the exception will be handled.

does this include when a method calls another one in a composition
situation?

that is method A::a calls method B::b which Calls method C::c that
throws, can I catch and handle inside A::a? if yes, this is
amazing. can you give an example please?
thanks
Your understanding of my description of exception handling is
completely accurate. Here is simple example using global functions but
member routines would work just the same:

#include <iostream>

int c()
{
throw int(5);
}

int b()
{
c();
return 1;
}

int a()
{
b();
return 0;
}

int main()
{
try
{
a();
}
catch (int& i )
{
std::cout << "Caught exception: " << i << "\n";
}
}

Program Output:

Caught exception: 5

Greg

Dec 23 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
1
by: Diego Mijelshon | last post by:
While the XmlSerializer can read boolean values as either 0/1 or true/false, it always writes true/false, which is a problem when I want to insert in an SQL Server bit column. Temporarily, I'm...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
43
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. ...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have the method below that returns a bool, true or false depending on if the conversion to date tiem works. It takes a string input. I am only returning the bool but would also like to...
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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,...

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.