bool method() throw in conditional statement

G

Gary Wessle

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
 
G

Greg

Gary said:
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
 
C

Clark S. Cox III

Gary said:
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 ... */
}
 
G

Gary Wessle

Greg said:
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
 
B

BobR

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::eek:stream &cout ){
// ------------
cout<<"\n--- Exception test Bumfaddle ---"<<std::endl;

try{
BumFaddle Bf;
Bf.funcy( cout );
std::vector<int> Vint(2);
Vint.at(3); // out_of_range
} // try
catch( const std::eek:ut_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::eek:stream &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?
 
G

Greg

Gary said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,224
Latest member
BettieToom

Latest Threads

Top