catch(Exception* e){ throw;} generate error

Discussion in 'C++' started by Daimler, Jan 25, 2007.

  1. Daimler

    Daimler Guest

    i am using visual c++ 2005 clr window form with

    try and catch a serial port readline.

    the codes are as follows: -

    try{

    Form1::textBox1->Text = Form1::serialPort1->ReadLine();

    }

    catch(Exception* e){ throw;}



    ------------------------------------------------------------------------------------------

    the debug generate an error.

    what is wrong?

    please help!

    daimler_trying_hard
     
    Daimler, Jan 25, 2007
    #1
    1. Advertising

  2. Daimler

    Colander Guest

    On Jan 25, 2:09 pm, "Daimler" <> wrote:
    > i am using visual c++ 2005 clr window form with
    >
    > try and catch a serial port readline.
    >
    > the codes are as follows: -
    >
    > try{
    >
    > Form1::textBox1->Text = Form1::serialPort1->ReadLine();
    >
    > }catch(Exception* e){ throw;}
    >
    > ------------------------------------------------------------------------------------------
    >
    > the debug generate an error.
    >
    > what is wrong?


    Most probably the ReadLine() fails and trows an error that your catch
    will catch
    but there it gets retrown and probably no other catch exists so an
    uncaught exeption
    exists and your debugger will tell you this is an error.

    If this is not the case, I advise you to take your problem to an MS
    related
    newsgroup, they will understand better what the ReadLine() and other
    elements
    of your code are doing, we only talk about 'pure' C++ here.

    Good luck,
    Bas
     
    Colander, Jan 25, 2007
    #2
    1. Advertising

  3. Daimler

    benben Guest

    Daimler wrote:
    > i am using visual c++ 2005 clr window form with
    >
    > try and catch a serial port readline.
    >
    > the codes are as follows: -
    >
    > try{
    >
    > Form1::textBox1->Text = Form1::serialPort1->ReadLine();
    >
    > }
    >
    > catch(Exception* e){ throw;}
    >
    >
    >
    > ------------------------------------------------------------------------------------------
    >
    > the debug generate an error.
    >
    > what is wrong?


    We don't know. What error do you get? Why are you catching a pointer? By
    rethrowing the exception you are allowing it to propagate. So if there
    is nothing else to catch the exception the stack unwinding will pass
    through main() then crashes the program.

    >
    > please help!


    You need to post a minimal program written in ISO C++ without 3rd party
    library to demonstrate your problem. Then we can pinpoint it for you. If
    you find this hard to do, maybe it's just not a language problem and you
    should try elsewhere.

    >
    > daimler_trying_hard
    >


    Regards,
    Ben
     
    benben, Jan 25, 2007
    #3
  4. Daimler

    Guest

    On 25 Jan, 13:09, "Daimler" <> wrote:
    > i am using visual c++ 2005 clr window form with
    >
    > try and catch a serial port readline.
    >
    > the codes are as follows: -
    >
    > try{
    >
    > Form1::textBox1->Text = Form1::serialPort1->ReadLine();
    >
    > }catch(Exception* e){ throw;}
    >
    > ------------------------------------------------------------------------------------------
    >
    > the debug generate an error.
    >
    > what is wrong?
    >


    Well taking a few wild guesses.

    Form1::serialPort1 is a null or garbage pointer.

    Form1::textBox1 is a null or garbage pointer

    The object pointed to by Form1::serialPort1 has not been 'connected' to
    a serial port.

    etc. etc.

    Why do you complain that 'throw' generates an error, isn't that what it
    is supposed to do? What are you expecting to happen if ReadLine fails?

    John
     
    , Jan 25, 2007
    #4
  5. Daimler

    peter koch Guest

    On Jan 25, 2:09 pm, "Daimler" <> wrote:
    > i am using visual c++ 2005 clr window form with
    >
    > try and catch a serial port readline.


    Note that this forum is for standard C++ and not extensions or
    languages similar to C++.
    >
    > the codes are as follows: -
    >
    > try{
    >
    > Form1::textBox1->Text = Form1::serialPort1->ReadLine();
    >
    > }catch(Exception* e){ throw;}
    >
    > ---------------------------------------------------------------------------­---------------

    If the above is "pure C++", the code is technically healthy and should
    not generate an error. One should question thoush, why you catch by
    pointer and not by (const) reference.
    >
    > the debug generate an error.
    >
    > what is wrong?

    Depends on what error - you are not at all clear about this. I suggest
    you create a minimal working sample (in C++!) and show it to us. In
    that process you often discover the error by yourself.

    /Peter
     
    peter koch, Jan 25, 2007
    #5
  6. Daimler

    Ron Natalie Guest

    peter koch wrote:

    >> ---------------------------------------------------------------------------­---------------

    > If the above is "pure C++", the code is technically healthy and should
    > not generate an error. One should question thoush, why you catch by
    > pointer and not by (const) reference.
    >


    One possibility is the pointer he is throwing refers to an object that
    has ceased to be:

    BAD:
    try {
    Exception x;
    throw &x;
    } catch (Exception* x) { ...
     
    Ron Natalie, Jan 25, 2007
    #6
  7. Daimler

    benben Guest

    Ron Natalie wrote:
    > peter koch wrote:
    >
    >>> ---------------------------------------------------------------------------­---------------
    >>>

    >> If the above is "pure C++", the code is technically healthy and should
    >> not generate an error. One should question thoush, why you catch by
    >> pointer and not by (const) reference.
    >>

    >
    > One possibility is the pointer he is throwing refers to an object that
    > has ceased to be:
    >
    > BAD:
    > try {
    > Exception x;
    > throw &x;
    > } catch (Exception* x) { ...


    Well I guess you can safely throw bad pointers as long as you don't
    dereference them.

    Regards,
    Ben
     
    benben, Jan 25, 2007
    #7
  8. Daimler

    Daimler Guest

    thank you everyone for your advise.

    found the asnwers.

    using __try and __except to discard the exception dialog box.

    thanks again! -_-

    daimler_smiles

    "Daimler" <> wrote in message
    news:epa79i$841$...
    >i am using visual c++ 2005 clr window form with
    >
    > try and catch a serial port readline.
    >
    > the codes are as follows: -
    >
    > try{
    >
    > Form1::textBox1->Text = Form1::serialPort1->ReadLine();
    >
    > }
    >
    > catch(Exception* e){ throw;}
    >
    >
    >
    > ------------------------------------------------------------------------------------------
    >
    > the debug generate an error.
    >
    > what is wrong?
    >
    > please help!
    >
    > daimler_trying_hard
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
     
    Daimler, Jan 26, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,057
    Kevin Spencer
    Oct 27, 2003
  2. Jon Maz
    Replies:
    7
    Views:
    4,299
    Jon Maz
    Oct 25, 2004
  3. Matt
    Replies:
    1
    Views:
    343
    Andy Fish
    Jun 10, 2004
  4. Matt
    Replies:
    1
    Views:
    414
    Michael Rauscher
    Jun 12, 2004
  5. Replies:
    15
    Views:
    7,625
    Roedy Green
    Sep 8, 2005
Loading...

Share This Page