[CPP] Exceptions and Lambdas

How do we report errors?

  • Return an error value from function
  • set a global error code
  • Throw an exception

Exceptions

  • Exceptions are “thrown” to report exceptional circumstances
  • You can throw any type of object, fundamental type or pointer as an exception in C++
  • The standard class library provides some standard exception types, all derived from exception class
  • You add handler code to catch the exception
  • The stack frame is unwound, one function at a time (as if the functions returned immediately) until a catch which matches the type throw is found

Catching exceptions

  • First specify that you want to check for exceptions(try)
  • Then call the code which may raise the exceptions
  • Then specify which exceptions you will catch, and what to do
  • The same as Java

The catch clause

  • A catch clause will match an exception of the specified type
  • catch clauses are checked in the order in which they are encountered, the order of the catch clauses matters!
  • Pointers and objects are different
  • we can throw 1 or MyException ob; throw ob;
    enter image description here
Here we show different ways of throw and catch. You can throw a object like 1.2f or “string” or a pointer like new xxx. When you catch, you should also catch different types.
enter image description here

The catch clause and sub-classes

Sub-class objects are base class objects
  • catch clause will match sub-class objects
  • e.g. catch ( BaseClass & b){} will also catch sub-class objects
  • e.g. catch ( BaseClass * b){} will also catch sub-class objects
  • Remember reference and pointer are different!!!
  • There are some tricky part in exception, first is about baseclass and subclass, second is pointer and objects, so the order matter!
  • If you throw a base class object, only can be caught in base class
  • If you throw a sub class object, it can be caught by the first base or sub class, the order matters
  • If you throw a pointer, it can only be caught by the same class pointer

Aside: exceptions and throw()

throw at the end of the function declaration limits the exceptions which can be thrown
  • It is optional
  • In java, throws < type> is mandatory
If specified, then all exception types which can be thrown by the function must be specified:
  • void MyFunction(int i) throw (); Function will not throw exceptions;
  • void MyFunction(int i) throw (int); Function will only throw ints as exceptions;
  • void MyFunction(int i) throw (…); Function will throw any exception;
    Note:
  • Throwing an exception while there is an uncaught exception will end the program
  • No catching a thrown exception will end the program
  • Try to catch (and handle) an exception as close as possible to the place it was generated
  • Do not catch an exception if you cannot do something with it(leave it to your caller, this doesn’t mean not to catch it, remember we need to catch everything)
  • If you throw exceptions, prefer to throw standard class library exceptions, or sub-classes of these

Multiple Functions

enter image description here

The catch clause and sub-classes

Because subclass objects are base class objects, so catch clauses will match sub-class objects
enter image description here
From this point, there will be a lot trick questions
enter image description here
Here Sub2* ps2 is a pointer, so we should choose D

C++ 11 Lambdas

  • Lambdas are anonymous functors that get created on the fly
  • Specify what to capture when the lambda is created, like passing local/global variables into constructors
    • Pass variables in by value or by reference
    • Specify to capture specifics or all used
      • Return type: usually implicit (from type returned)
      • Basic Lambda function:
      • [Capture](parameters)-> return_type{function body}
      • e.g. [iAdd](int i) -> int { return i+ iAdd; }

Basic lambda function

enter image description here
The output will be 7 8 9 10 11 12 13

Captures

enter image description here
So we can change our lambda functions to:
enter image description here
And the result is the same

Why might lambdas be important?

enter image description here
Return value can be specify or not
  • any_of : If any of the element in vector satisfied the condition, then return 1 else return 0
  • similarly, there are none_of, all_of
  • find_if will find the iterator to first element which satisfied the condition, so we need a “*” before it

评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics