Re: [C++] help understaind the throw/try/catch mechanism
From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 08/20/04
- Next message: Old Wolf: "Re: [C++] help understaind the throw/try/catch mechanism"
- Previous message: Wei Li: "Is it possible to get the caller's name in C?"
- In reply to: Andrew Falanga: "[C++] help understaind the throw/try/catch mechanism"
- Next in thread: Old Wolf: "Re: [C++] help understaind the throw/try/catch mechanism"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Aug 2004 16:17:49 -0700
Andrew Falanga <falandr@hp.com> wrote:
>
> try {
> base b("string to initialize with");
> }
> catch(base::exp) {
> std::cout << "not gonna do it" << std::endl;
> }
>
> b.someFuncInB();
>
> Ok, it's in the call to b.someFuncInB that the compiler is having
> problems. It says the b is undeclared first use in this function. This
> must be resulting from the fact that b is in the try block and is going
> out of scope after the try block. However, why does the try section
> have scope like that? Admittedly, I'm not familiar with it, but why
> would I want to enclose everything dealing with b in the try block when
> only the constructor is going to throw the exception?
If that's really what you want, then you could do this:
std::auto_ptr<B> b_ptr;
try {
b_ptr.reset(new B("string"));
}
catch....
// if you get to this point and construction failed, b_ptr will still
// be NULL. Supposing you have checked that the construction worked,
// you could proceed:
B &b = *b_ptr;
// work with b...
- Next message: Old Wolf: "Re: [C++] help understaind the throw/try/catch mechanism"
- Previous message: Wei Li: "Is it possible to get the caller's name in C?"
- In reply to: Andrew Falanga: "[C++] help understaind the throw/try/catch mechanism"
- Next in thread: Old Wolf: "Re: [C++] help understaind the throw/try/catch mechanism"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|