Added snippet to demo exceptions.

This commit is contained in:
Donne Martin 2015-01-25 05:48:18 -05:00
parent a0f928a591
commit 6fa559064a

View File

@ -414,6 +414,93 @@
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exceptions"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def convert_to_float(x):\n",
" try:\n",
" x = float(x)\n",
" except ValueError:\n",
" print('Failed')\n",
" else: \n",
" # Executes only when try succeeds\n",
" print('Succeeded')\n",
" finally: \n",
" # Always executes\n",
" print('Completed convert_to_float')\n",
" return x"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = convert_to_float('7')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Succeeded\n",
"Completed convert_to_float\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": [
"7.0"
]
}
],
"prompt_number": 37
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = convert_to_float('a')\n",
"x"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Failed\n",
"Completed convert_to_float\n"
]
},
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 38,
"text": [
"'a'"
]
}
],
"prompt_number": 38
},
],
"metadata": {}
}