Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Exception Handling

Options
  • 21-03-2005 8:06pm
    #1
    Closed Accounts Posts: 6


    Hi all,

    I am writing a c++ program that involves exception handling.I am reading in a int value from the user.How can i make sure that they are not inputting a character value.

    I have tried checking this value with the isdigit predefined function ctype.h library.IE if the value is not a digit i throw an exception

    Code:
    if(!isdigit(y))
    {
    throw " Sorry you have entered a character.This is not allowed!!";
    }

    catch (char *str)
    {
    cout << endl<< "Exception: " << str << endl<<endl;
    cout<<"Please try again the Program again!"<<endl;
    exit(0);
    }

    where y is the integer value entered by the user.

    Any help with this would be greatly appreciated,
    Thanks,
    Captain P


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    the exception will only be caught if it's thrown within a try block.

    So something like this:

    try{
    doDangerousThing(); // Throws an exception
    doSomethingElse();
    }catch(char *str)
    {
    cout << endl<< "Exception: " << str << endl<<endl;
    cout<<"Please try again the Program again!"<<endl;
    exit(0);
    }

    If it's anything complicated, you might want to consider creating your own Exception objects, so you can quickly see what's going wrong and handle it accordingly.


Advertisement