By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,122 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,122 IT Pros & Developers. It's quick & easy.

bool method() throw in conditional statement

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.