throwing structures

This is a discussion on throwing structures within the C++ Programming forums, part of the General Programming Boards category; Hi, I am throwing a structure from within my class which contains specific error information. Would anyone be able to ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    throwing structures

    Hi,

    I am throwing a structure from within my class which contains specific error information.
    Would anyone be able to tell me if it is stack safe or not. Because I declare an instance
    of the structure on the stack, and then assign data to the members of the struct. For example,
    this wont compile... just for illustration purposes. Is it safe to process the error in this way?

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct ErrorInfo
    {
      CString cStrErrorMsg;
      CString cStrAdditional;
    };
    
    func1(void);
    func2(void);
    
    int main(void)
    {
      try
      {
        func1();
      }
      catch(ErrorInfo error)
      {
        cout << error.cStrErrorMsg << " " <<  error.cStrAdditional << "."
      }
    
      return 0;
    }
    
    func1(void)
    {
      try
      {
        func2();
      }
      catch(ErrorInfo error) throw;
    }
    
    func2(void)
    {
      ErrorInfo error
    
      error.cStrErrorMsg = "Error Occured";
      error.cStrAdditional = "Nothing";
      throw error;
    }
    Be a leader and not a follower.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is it safe to process the error in this way?
    Not if it doesn't compile it isn't.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    That was just an example of similar behaviour of how my classes deal with errors. I am able to raise exceptions from 5 levels deep, and the data always seems to be consistant when it gets to the final catch block (This is not normal!, just for testing purposes). So I am presuming this method is safe for handling the errors, and even though the structure was declared on the stack, the throw keyword must make another copy of the structure being thrown some how. Otherwise, the data would be invalid from the stack unwinding.
    Be a leader and not a follower.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Yes, that method of throwing exceptions is safe, and is probably one of the better ways of doing exception handling. I would recommend catching the exception structure by reference, though, because an extra copy is made automatically first before reaching the catch block.

    This code compiles and works as I expect you'd expect:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct ErrorInfo
    {
      string cStrErrorMsg;
      string cStrAdditional;
    };
    
    void func1();
    void func2();
    
    int main(void)
    {
      try
      {
        func1();
      }
      catch(ErrorInfo& error)
      {
        cout << error.cStrErrorMsg << " " <<  error.cStrAdditional << ".";
      }
    
      return 0;
    }
    
    void func1()
    {
      try
      {
        func2();
      }
      catch(ErrorInfo& )
      {
        throw;
      }
    }
    
    void func2()
    {
      ErrorInfo error;
    
      error.cStrErrorMsg = "Error Occured";
      error.cStrAdditional = "Nothing";
      throw error;
    }

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Ok, thanks for the reply. Thats what I was looking for.... an answer to my question.
    Be a leader and not a follower.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Even if you don't put func2() in a try-catch block in func1(), would main() still catch the exception? I think I heard from someone that a thrown exception will automatically make its way down the stack until a function catches it somewhere along the line. Is it really necessary for the intermediate try-catch if all it does is throw the exception again anyway?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    Yes, the exception will be catched by main if there is no catch block in func1().

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21