Don't understand try-catch/if-else-throw

Im working on a program that is a rectangle class, and it has three files and runs fine but i need to implement an if-else-throw in the set function and try-catch in main to print when input is invalid.

Ive looked into if else throw and dont understand how to use it exactly in my program
Can anyone help by demonstrating how a if-else-throw/try-catch works because i can not grasp the concept of it. Thank you and and all help is greatly appreciated.
main cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream> 
#include "Rectangle.h" 

using namespace std; 
int main () 
{ 

//unit rectangle 
Rectangle unitRectangle; 

//rectangle initialized with length =2.0 & 
//width = 5.0 
Rectangle myRectangle(2.0,5.0); 

cout <<"Displaying the statistics of unit rectangle :"<<endl; 

unitRectangle.displayStatistics (); 

cout <<endl<<"Now displaying the statistics of myRectangle :"<<endl; 

myRectangle.displayStatistics (); 

/* Variables to take user input */ 
double num1,num2; 

cout <<endl; 
cout <<"Enter the length and width of a rectangle followed by space:"; 
cin>>num1 >> num2; 

myRectangle.setLength(num1); 
myRectangle.setWidth (num2); 

cout <<endl <<"Now displaying the statistics of the new Rectangle with "; 
cout <<"Users input "<<num1<<" and "<<num2<<": "<<endl; 

myRectangle.displayStatistics (); 
cout <<endl; 
return 0; 
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/doc/tutorial/exceptions/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <stdexcept>

struct Rectangle
{
    Rectangle( double length, double width ) : length_(length), width_(width)
    {
        throw_if_invalid() ; // if invalid length/width, throw to report error
        std::cout << "constructed Rectangle " << length << " x " << width << '\n' ; // ok
    }

    double length() const { return length_ ; }
    double width() const { return width_ ; }

    void length( double new_length )
    {
        if( valid_dimension(new_length) ) length_ = new_length ; // if new_length is valid
        else throw std::domain_error( "invalid length" ) ; // not valid; throw to report error
        // note: std::domain_error indicates that input is outside the domain of the function (operation)
        // http://en.cppreference.com/w/cpp/error/domain_error
    }

    void width( double new_width )
    {
        if( valid_dimension(new_width) ) width_ = new_width ; // if new_width is valid
        else throw std::domain_error( "invalid width" ) ; // not valid; throw to report error
    }

    private:
        double length_ ; // invariant: length > 0
        double width_ ; // invariant: width > 0
        static bool valid_dimension( double value ) { return value > 0.0 ;  }

        bool valid() const // returns true if the object is in a valid state (class invariants hold)
        { return valid_dimension( length() ) && valid_dimension( width() ) ; }

        void throw_if_invalid() const
        {
            if( !valid() ) // if the invariant is violated
                throw std::domain_error( "invalid length/width" ) ; // throw an exception to indicate failure
        }
};

int main()
{

    try
    {
        std::cout << "try to construct rectangle 22.3 x 7.8\n" ;
        Rectangle one( 22.3, 7.8 ) ;
        std::cout << "ok\n\n" ;

        std::cout << "try to construct rectangle 22.3 x -7.8\n" ;
        Rectangle two( 22.3, -7.8 ) ; // constructor will throw
        std::cout << "ok\n\n" ;
    }

    catch( const std::domain_error& e ) // catch the error
    {
        std::cerr << "*** error: " << e.what() << "\n\n" ; // and (trivially) handle it
    }

    try
    {
        Rectangle three( 0.1, 0.1 ) ;

        std::cout << "try to set width to 4.5\n" ;
        three.width(4.5) ;
        std::cout << "ok\n\n" ;

        std::cout << "try to set width to -1.2\n" ;
        three.width(-1.2) ; // invalid width, this will throw
        std::cout << "ok\n\n" ;
    }
    catch( const std::domain_error& e ) // catch the error
    {
        std::cerr << "*** error: " << e.what() << '\n' ; // and (trivially) handle it
    }
}

http://coliru.stacked-crooked.com/a/0e701445de714c3a
Topic archived. No new replies allowed.