Re: [C++] help understaind the throw/try/catch mechanism

From: Old Wolf (oldwolf_at_inspire.net.nz)
Date: 08/20/04


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...



Relevant Pages

  • Re: [C++] help understaind the throw/try/catch mechanism
    ... Andrew Falanga wrote: ... it's in the call to b.someFuncInB that the compiler is having ... > out of scope after the try block. ... Supposing you have checked that the construction worked, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: identity...... Was: The wisdom of the object mentors
    ... And what about construction of int? ... I construct all objects in the same manner.....(like a C++ compiler ... in mid air...I need a language, some axioms and some rules of ...
    (comp.object)
  • Re: identity...... Was: The wisdom of the object mentors
    ... And what about construction of int? ... I construct all objects in the same manner.....(like a C++ compiler ... buy it anyway, because A is a subtype, ...
    (comp.object)
  • Re: Example interpreter C
    ... Henry Butwsky schrieb: ... > I have written a basic parser that evaluates ... Hm, I don't have an Interpreter, but we made a compiler in the ... compiler for a subset of C construction course (we used the java ...
    (comp.compilers)
  • low-level pointer vs. array question
    ... Unforuntately, I know next to nothing about ASM and compiler construction, ... I am not sure I really understand this low-level difference between pointers ... and arrays, and I do not have the time to learn ASM and compiler ...
    (comp.lang.c)