> I have a static method in a class that parses a text string.
> It basically breaks the text string down into segments and then calls
> further static methods to do what it has to with those segments.  If
> any part of the parsing fails in those sub-methods, they throw a user
> exception using AfxThrowUserException().
>
> Then, the main static Parsing method catches the user exception, and
> assigns the return value to be FALSE.  Hence the main calling code
> displays an error message to the user saying that parsing failed.
>
> That works fine.  The question is, how do I go about having an
> exception thrown that is exactly the same, but lets me give it some
> kind of error value. Eg:
>
> If(!parsestring(string, structure, myownexception)) {
>       display error message myownexception.geterrorno }
>
> I don't even know if I need to go this far. Can I simply use
> SetLastError and GetLastError using my own values?  I have used
> GetLastError before, but only on system methods that use it.

You can use SetLastError and GetLastError, but I don't suggest them.  I
suggest you just feed an error code into the exception, like:

enum ExceptionCode
{
        // ...
};

class Exception
{
public:
        Exception(ExceptionCode code)
                : m_code( code )
        {
        }

        ExceptionCode GetCode() const
        {
                return m_code;
        }

private:
        ExceptionCode m_code;
};

void parser_internal()
{
        if (something_bad())
        {
                throw Exception( theErrorCode );
        }
}

void parser_external()
{
        try
        {
                parser_internal();
        }
        catch (Exception & ex)
        {
                ExceptionCode = ex.GetCode();
                // do something with it
        }
}

-------------
Ehsan Akhgari

Farda Technology (http://www.farda-tech.com/)

List Owner: [EMAIL PROTECTED]

[ Email: [EMAIL PROTECTED] ]
[ WWW: http://www.beginthread.com/Ehsan ]

He who sees the abyss, but with eagle's eyes - he who with eagle's talons
grasps the abyss: he has courage.
-Thus Spoke Zarathustra, F. W. Nietzsche




_______________________________________________
msvc mailing list
[EMAIL PROTECTED]
See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for subscription 
changes, and list archive.

Reply via email to