{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Operations\n", "- try / except / finally\n", "- raise\n", "- assert\n", "- with / as\n", "\n", "# Usage\n", "- Error Handling\n", " - Catch and response to errors\n", " - Ignore errors (or Python will trigger default handling behavior)\n", "- Event Notification\n", " - Singal valid condition without pass result flags\n", "- Special-case Handling\n", "- Termination Actions\n", " - Guarantee closing-time operation be performed (**with/as** can be an alternative)\n", "- Unusual Control Flow\n", " - High-Level and structured \"go to\" (**raise** can be used as jump)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "ename": "NameError", "evalue": "name 'a' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Default Exception Handler\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined" ] } ], "source": [ "# Default Exception Handler\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is not defined\n", "Runs no matter exception occur or not\n" ] } ], "source": [ "try:\n", " a\n", "except NameError:\n", " print(\"a is not defined\")\n", "finally:\n", " print(\"Runs no matter exception occur or not\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- There is no way in Python to go back to the code that triggered the exception\n", "- **assert** is a conditional **raise**, used mostly for debugging" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "assert 1 == 1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "ename": "AssertionError", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAssertionError\u001b[0m: " ] } ], "source": [ "assert 1 == 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Context Management (with/as)\n", "\n", "- An alternative to **try / except **, but can only be used for some types of oject\n", "- Guarantee termination actions\n", "- It supports user-defined contect mangement" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with open(\"../README.md\") as f:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above code perform both **open()** and **close()**" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }