try/catch/throw exceptions

This is a discussion on try/catch/throw exceptions within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a program where I am calculating a number and then taking the inverse of it (i.e: 1/x), ...

  1. #1
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    try/catch/throw exceptions

    Hello,

    I have a program where I am calculating a number and then taking the inverse of it (i.e: 1/x), I am going through this calculation many times...but there are times when x=0, which will make it not possible to take the invese of(i.e 1/0)......I am attempting to use a try /catch/throw method....my function's name is solver, and it is being called in main:

    int main(){

    try { solver(); }
    }

    now the function:

    void solver(){

    //this is where my ? is, I just need to check is if x == 0??

    if (x == 0){

    //is this exception, or do I write invalid_argument or range_error
    //or is it basically anything I want it to be, are they biult in?

    throw_exception("x found to be 0"
    }

    //now back to main
    int main() {
    try { solver(); }

    catch(exception ){

    cout<<"error, 0 calculated<<endl;}//end catch

    }

    I am getting a many errors with this...and since I've never used try/catch/throw...I am not sure what I am doing wrong...

    also, when it does catch an exception, does it exit the program?
    or is there a way to catch the exception and go to the next iteration and next calculation?? thanks for any help...

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The syntax should be something like -

    Code:
    #include <iostream>
    
    using namespace std;
    
    void solver(int x){ 
    	if (x == 0)
           throw ("x found to be 0") ;
    }
    
    int main() { 
    	try { solver(0); } 
    	catch(char* a){ 
    	cout<<a << '\n' ; //end catch 
    	} 
    	return 0;
    }
    If you don't wish to exit the program you'll either have to call the solver function again after it has thrown or move your try/catch block into the solver function.

  3. #3
    smd
    smd is offline
    Registered User
    Join Date
    Jun 2002
    Posts
    43

    having problems with try/catch throw

    hello...

    I'm having some trouble figuring out why my code is not catching exceptions...

    I have tried the syntax suggested above...but nothing is getting printed to the screen about getting the 0...this is the code:


    int main(){

    makesolver sheet(); }


    void makesolversheet() {

    get();

    bestfit(); //this is the function that is calculating a slope value

    }//end makesolversheet


    void bestfit() {

    --some calculations--

    if (slope == 0){

    try { tryslope(slope); }//if slope is 0

    }//end try

    catch (char* a) {

    cout <<a<<'\n'; } //end catch

    }//end bestfit



    void tryslope(float x) {

    if (x == 0)
    { throw ("slope calc. to be 0"); }


    }//end tryslope


    I have assigned in my code for slope to be 0 so that it enters the loop in the bestfit function and tryslope gets called...however when I run it...the statement: "slope calc to be 0" is not showing up... ( My main need is to be able to go through many calculaiton of the slope...when 0 is caluculated...let the user know and move on to the next calculation...not exiting the program)...however this is not even printing that 0 was calc...any help will be greatly appreciated... thanks

  4. #4
    Unregistered
    Guest
    here's a reference site that discusses try/catch. May not be the best explanation, but it's a place to start.

    http://www.stud.fim.ntnu.no/~oystesk...0.htm#Heading2


    based on that reference and on how I interpret your code I would consider doing something like this:


    //declare the exception class before main()
    class zeroDenom {};

    int main(){

    makesolver sheet(); }


    void makesolversheet() {

    get();

    bestfit(); //this is the function that is calculating a slope value

    }//end makesolversheet

    try{
    void bestfit() {

    --some calculations--
    if(x == 0)
    throw zeroDenom();
    //more calculations
    } //end bestfit()
    }//end try

    catch (zeroDenom) {

    cout << "cannot divide by zero" << '\n'; } //end catch

    }//end specific catch
    catch(...)
    {
    cout << "unexplained failure" << endl;
    }//end non-specific catch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are Exceptions effective?
    By Petike in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2008, 01:23 AM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 03:12 AM
  3. Exceptions and constructors
    By Mostly Harmless in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2007, 12:20 PM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 02:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 10:21 AM

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