Lecture 8

SFR1811·2022년 1월 28일
0

3.11 Coroutine

There are many languages that has coroutine.
Coroutine comes in two forms:

  1. stackless: uses callers stack
  2. stackful: creates its own stack

Python has stackless, semi coroutine.
In python, coroutine does not come as a form of class.
It is single interface, and you can't make calls from it.

def Fibonacci(n):
  fn = 0; fn1 = fn
  yield fn
  fn = 1; fn2 = fn1; fn1 = fn
  yield fn
  #whild True:
  for i in range(n-2):
    fn = fn1 + fn2; fn2 = fn1; fn1 = n
    yield fn

4 Exception

4.1 Derived Exception-Type

C++ didn't start with hierarchical exception, but laguages like JAVA started with one.
This helps step by step exception handling.

You want to catch exception by reference because if exception is caught by value, there may be truncation happening.

4.4 Exception List

program keeps all exceptions, and the program can statically or dynamically tell if the exception is handled or not

  1. static way: uncaught exceptions are detected at compile time - Java way
  2. dynamic way: if unhandled exception is thrown and detects at runtime, it converts into special failure exception. - C++ way

Why is c++ not implementing static unhandled exception catch?
It prevents code reuse

it gets impossible to expect for exception if concurrency is involved

4.6 Multiple Exceptions

struct E{};
int cnt = 3;
void f(int i){
  if (i == 0) throw E();
  try {
    f(i-1);
  }catch (E) {
    cnt -= 1;
    if (cnt>0) f(2);
  }
}
int main(){f(2);}

A stack can hold arbitrary number of exceptions in a stack.
However, multiple exceptions cannot propagate simultaneously
Destructor is called during propagation, so if you were to throw an exception in destructor, you need to call it in a block that is only called if it's not propagating.

profile
3B CS

0개의 댓글