Level 10 : Handling exceptions inside a generator.


def safe_gen():
    try:
        yield 1
        1 / 0
        yield 2
    except ZeroDivisionError:
        yield "Error caught"
    yield 3

print(list(safe_gen()))

Author: SandhyaR | Tags: generator | 1736+ Python FAQs | Share
python-repl | frozenset | lambda-function | call | integer | library | bitwise-operator | list-comprehension | random | split | installation | descriptors | software-design | string-slicing | sorted | functional-programming | factorial-function | encapsulation | for | application | match | package-management | typeerror | update | module | python-history | control-statement | list-operation | assignment-operator | file_handling | complex | global-variable | numeric | keyword | bitwise-operator | center | datetime | error-handling | deep-copy | pseudocode | loop | dicts | collections | syntax-error | class | string | lambda | logical-operator | zip | thread | array | zip-function | version | gil | immutability | while-loop | memory | formal-and-actual-parameter | data-structures | threading | regular-expression | sys | type-conversion | exception-handling | operator-overloading | error | destructor | magic-method | input-function | virtual-environment | data-type | complex-number | matrix-list-comprehension | generator | mysql | statement | id-function | built-in-function | global | bitwise | operators | __future__ | conversion | expand-tab | python-module | operator-and | string-concatenation | python | generators | memory-management | overflow | sum-function | functions | control-flow | copy | operand | global-variable | else-statement | format | string-formatting | with-statement | __init_subclass__ | control-flow | identifier | type_conversion | condition | immutable-data-type | basic | odd-number | staticmethod | rollback | binary-operation | matrix | function-argument | syntaxerror | range | string-method | file-handling | data-structure | pickling-&-unpickling | precedence | default-mutable-argument | asyncio-module | print | scientific-notation | python_version | counter-function | string-formatting | operations | swapping | nested-loop | file-handling | mathematics | conditional-statement | lambdas | built-in-function | python-django | input | round | global-keyword | postfix | attribute | compute | loop-control | map-function | tuple-unpacking | touples | filter-function | oops | multiple-inheritance | polymorphism | inherit | round-function | mapping-function | iterable | sys-module | higher-order-function | dictionary | escape-operator | __all__-attribute | data_structure | min-max-function | slicing | comparison | goto-function | operator-or | class-and-object | list-mutation | syntax | precedence-and-associativity | object-attribute | generators-in-python | indexing | lists | filter | decorator | version-check | join | list-slicing | classes | list | class-and-inheritance | constructor | head-and-tail | floating-point-precision | data-structure | zfill-function | exponentiation | collection | enumerate() | multiprocessing | boolean-and-non-boolean | pandas | method | variable | virtual-environment | indentation | mathematical-function | built-in | string-method | list-comprehension | sum() | comprehension | command-line-parsing | data-type | set | multithreading | type-conversion | name-mangling | turtle | access-modifier | mutable | regex | set-operation | methods | init | expression | asyncio | raise | parameter | class-and-instance-variable | identity-operator | string-manipulation | operator | return-statement | recursion | file | string-slicing | shallow-copy | comment | feature | fabs-function | syntax-error | while-and-for-loop | docstring | min-function | assert | design-principle | numpy | arithmetic-operation | debugging | int | random-function | interpreter | async | exception-handling | list-method | append | benefit | logical-operator | max-min | arithmetic-expression | yield | boolean | namedtuple | recurssion | choice | list-vs-tuple | def | heap-memory | tuple | loops | sorting | context-manager | function-and-scope | math | matplotlib | encoding | decimal | import-method | identity-operator | exception | stack | dictionaries | logical-operation | counter | while-else-loop | input-function | function | pep | list-indexing | static-method | namespace | oop | list-join | binary | invalid-list | implementation | inheritance | string-iteration | print-statement | objects | conditional-statement | math-copysign | datatype | iterator | slice ...