pr3/python/ausnahmen/ausnahmen.ipynb

67 lines
1.2 KiB
Plaintext
Raw Normal View History

2023-06-23 23:05:46 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Division by zero caused by 3, 0\n"
]
}
],
"source": [
"# Try ... except\n",
"\n",
"def div(x,y):\n",
" try:\n",
" return x / y\n",
" except ZeroDivisionError:\n",
" print(\"Division by zero caused by {0}, {1}\".format(x,y))\n",
"\n",
"div(3,2)\n",
"div(3,0)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Ausnahme selbst werfen\n",
"\n",
"def div(x,y):\n",
" if y == 0:\n",
" raise ZeroDivisionError(\"Wert für Division ungültig\")\n",
" return x / y"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.10.9"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}