\( % theorem \newenvironment{pf}{\textbf{Proof.}}{\rule{1ex}{1ex}} \newtheorem{qu}{Question} \newtheorem{thm}{Theorem} \newtheorem{rmk}{Remark} \newtheorem{go}{Goal} \newtheorem{df}{Definition} \newtheorem{prop}{Proposition} \newtheorem{mot}{Motivation} \newtheorem{ex}{Example} \lstnewenvironment{code}{}{} \newcommand{\nc}{\newcommand} %% env \nc{\env}[2]{ \begin{#1} #2 \end{#1} } \nc{\arr}[2]{ \begin{array}{#1} #2 \end{array} } \nc{\arrb}[2]{ \left\{ \begin{array}{#1} #2 \end{array} \right. } \nc{FTC}[3]{ \left[#3\right]^{#2}_{#1} } \nc{\bracs}[1]{ \left[#1\right] } \nc{\brac}[1]{ \left(#1\right) } \nc{\bracp}[1]{ \left\{#1\right\} } \nc{\intd}[4]{\int^{#2}_{#1}\left(#3\right) #4} % shortcut \nc{\Lra}{\Leftrightarrow} \nc{\Ra}{\Rightarrow} \nc{\La}{\Leftarrow} \nc{\C}{\mathbb{C}} \nc{\R}{\mathbb{R}} \nc{\Fc}{\mathcal{C}} \nc{\bds}{\boldsymbol} \nc{\ds}{\displaystyle} % begin end \nc{\bit}{\begin{itemize}} \nc{\eit}{\end{itemize}} \nc{\bcode}{\begin{code}} \nc{\ecode}{\end{code}} \nc{\bsage}{\begin{sageblock}} \nc{\esage}{\end{sageblock}} %pic \nc{\pic}{\includegraphics} \nc{\svg}{\includesvg} \nc{\pica}{\includegraphics[width=100px,height=100px]} \nc{\picb}{\includegraphics[width=150px,height=150px]} \nc{\vs}{\vspace} \nc{\tbf}{\textbf} %arrow \nc{\upa}{\nearrow} \nc{\downa}{\searrow} \nc{\upw}{\rcurvearrowright} \nc{\downw}{\curvearrowright} \)

Ch 5 Exception

Compile time

  • Syntax error, type errors

Run-time error

  • detected by the computer
  • detected by a library
  • detected by user code

Logical error(?)

Run-time error

Find (possible) errors.

Possible errors

  • negative or zero area?
  • zero divisor.

who will handle negative or zero area

  • Caller(5.5.1): Main function
  • Callee(5.5.2): area, framed_area

Caller for area

if(x<=0) error("non-positive x");
if(y<=0) error("non-positive x");

We will see what the error function means later.

Callee for framed_area

int framed_area(int x, int y){
    constexpr int frame_width=2;
    if( x-frame_width<=0 || y-frame_width<=0 ){
        error("error called by framed_area()");
    }// Error handling
    return area(x-frame_width,y-frame_width);
}

Apply previous callee's solution to area()

int area(int length, int width){
    // Error handling
    return length*width;
}

Definition of error

inline void error(const string& s)
{
    throw runtime_error(s);
}

http://www.stroustrup.com/Programming/std_lib_facilities.h

Exception

If a function finds an error that it cannot handle, it does not return normally, instead it throws an exception.

try{

    // Something wrong! Throw an exception.

}
catch(Exception-type1 & e ){
    // Do something
}
catch(Exception-type2 & e ){
    // Do something
}
...

Example

Someone uses cerr instead of cout to report an error.

Check: With an exception of type out_of_range, try and catch the following out_of_range exception.

Scope(8.4)

  • global scope
  • namespace scope
  • class scope
  • local scope: { } ,parameters in function
  • statement scope : for

Example scope1

void f(int x){ //f:global, x: local
    int z=x+7; //z:local
}
void g(int x){  //g: global, x: local
    int f=x+2; //f: local
    return 2*f;
}

Fun questions

Make a max and abs(absolute value) function.

int max(int a, int b);
int abs(int a);

$$abs(x)=|x|$$

Example scope2

class Hello{
    private:
        vector<int> v; // v: class scope
    public:
        int largest()// As absolute values
        {
            int r=0; //r:local
            for(int i=0;i<v.size();i++){ //i: for-statment scope
                r=max(r, abs(v[i]));
            }
            return r;
        }
        // No r here.
};
//No v here

Example scope3

int x;// global x
int y;// global y
int f(){
    int x; //x: local, hide global x
    x=7;
    {
        int x=y;//local x, hide the previous local x
        ++x;
    }
    ++x;
    return x;
}

A common mistake on functions

void f(){
    void g(){

    }
}

Nested function is not legal in c++ (except lambda).

[8.5] function call and return

declaration

double f(int a, int b);

definition

double f(int a, int b){return a*b;}

void

void g(int a);

void means "does not return a value".
Here name a,b are not important when you implement.

Example: my_find in [273p]

my_find searches for s in vs starting at hint

int my_find( vector<string> vs, string s, int hint){
    if(hint < 0|| hint > vs.size()) hint=0;
    for(int i=hint;i<vs.size();++i)
        if(vs[i]==0) return i;
    if(hint > 0){ //Check before hint.
        for(int i=hint;i vs.size();++i)
            if(vs[i]==0) return i;
    }
    return -1; //Not found.

}

If you just do not want to use a hint, we leave 3rd argument as unnamed.

int my_find( vector<string> vs, string s, int){

    for(int i=-;i<vs.size();++i)
        if(vs[i]==0) return i;
    return -1; //Not found.

}

Pass by value

Check.Make a print function which prints values of 'vector' as the following way.

vector<double> v {1,2,3,4,5};
print(v);
// (1,2,3,4,5)

Return by reference

void print(const vector<double> & v)

What is benefit here?

homework

Make a swap function which swaps two int values.

int a=2, b=3;
swap(a,b);
//a=3, b=2