mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Added snippet to demo exceptions.
This commit is contained in:
parent
a0f928a591
commit
6fa559064a
|
@ -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": {}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user