Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Does anyone know how to create and throw their own exceptions from scratch in both MFC and Non MFC Code. Is there a Seminal article on this Subject! Microsoft does not seem to want to spill an awfull lot of beans on this subject.

What I'm looking for is something likethe following Pseudo Code:

C#
enum{
    EXCEPT_MEMORY=1000,
    EXCEPT_NO_SUCH_ACCOUNT=2000,
    EXCEPT_SESSION_TIMED_OUT=3000,
    EXCEP_BAD_PASSWORD=4000;
};

int SomeFunction(){
    if(AllocFails())throw(EXCEPT_MEMORY);
    if(!GetSession())throw(EXCEPT_SESSION_TIMED_OUT);
    if(!FindAccount())throw(EXCEPT_NO_SUCH_ACCOUNT);
    if(!CheckPassword())throw(EXCEP_BAD_PASSWORD);
    .
    .
    .
    // Do the deed, which may cause more exceptions
    .
    .
    .
    return iResult;
}
int AnotherFunction(){
    int Result;
    try{
        return SomeFunction();
    }
    catch(EXCEPT_SESSION_TIMED_OUT){
        SendErrorPage(ID_SESSION_TIMED_OUT);
        return FALSE;
    }
    catch(EXCEPT_NO_SUCH_ACCOUNT){
        SendErrorPage(ID_BAD_LOGONDATA);
        return BAD_RESULT;
    }
    catch(EXCEP_BAD_PASSWORD){
        SendErrorPage(ID_BAD_LOGONDATA);
        return BAD_RESULT;
    }
}
int BaseFunction(){
    int Result;
    try{
        Result=AnotherFunction();
        return Result;
    }
    catch(EXCEPT_MEMORY){
        SendErrorPage(ID_PAGE_BUSY_TRY_LATER);//Never Returns
    }
    catch(...){
        SendErrorPage(ID_TRY_AGAIN_CONTACT_ADMIN);//Never Returns
    }
}



In other words, Use the Exception mechanism to distinguish between normal program failures, and Resource Failures, and give the correct response each time.

Am I using this mechanism correctly now ?
Posted
Updated 19-Mar-11 15:55pm
v2

You should rather ask how exception work and how to process them.

What you ask is not a question: you can throw any object of any type, your own or not:

C++
throw 9000;
throw "some stupid immediate constant";
throw SomeInstanceOfMySophisticatedClass;
throw MySophisticatedClass(
    "some stupid immediate constant used to construct my class");


(This is so with C++. With most other languages you most usually can throw (or raise, another term for exact same thing) only the classes derived from a single class used as the only valid base class for all exception types.)

Will it help you? I think you should understand background.
I'm not familiar with any literature on basics; you can start here:

http://en.wikipedia.org/wiki/Exception_handling[^].

You should be comfortable with exception techniques: they are so important that these days programming without exception skill would be practically useless activity.
 
Share this answer
 
v2
Comments
Andrew Brock 17-Jan-11 22:07pm    
Most C++ programmers still don't use exceptions
Sergey Alexandrovich Kryukov 17-Jan-11 22:11pm    
May be. This is sad. This is because most of C++ programmers (and not only C++) are not real programmers.
Why was this Message Deleted by Chriss Mauder??
 
Share this answer
 
Comments
Indivara 20-Mar-11 0:11am    
You should add a comment, not another solution (and/or modify the question)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900