Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,67 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "cc2c441d-9dcf-4817-b097-cf6cbe440846",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: 5.0\n",
"Division operation completed.\n",
"Error: Division by zero is not allowed.\n",
"Division operation completed.\n"
]
},
{
"ename": "ValueError",
"evalue": "Invalid input: Division by zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 4\u001b[0m, in \u001b[0;36mdivide_numbers\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m 3\u001b[0m \u001b[38;5;66;03m# Code that may raise an error\u001b[39;00m\n\u001b[1;32m----> 4\u001b[0m result \u001b[38;5;241m=\u001b[39m a \u001b[38;5;241m/\u001b[39m b\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m:\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# Handle specific exception\u001b[39;00m\n",
"\u001b[1;31mZeroDivisionError\u001b[0m: division by zero",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[1], line 22\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[38;5;66;03m# Example usage:\u001b[39;00m\n\u001b[0;32m 21\u001b[0m divide_numbers(\u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m2\u001b[39m) \u001b[38;5;66;03m# Successful division\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m divide_numbers(\u001b[38;5;241m10\u001b[39m, \u001b[38;5;241m0\u001b[39m) \u001b[38;5;66;03m# Division by zero error\u001b[39;00m\n\u001b[0;32m 23\u001b[0m divide_numbers(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mten\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n",
"Cell \u001b[1;32mIn[1], line 8\u001b[0m, in \u001b[0;36mdivide_numbers\u001b[1;34m(a, b)\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mZeroDivisionError\u001b[39;00m:\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# Handle specific exception\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: Division by zero is not allowed.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m----> 8\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid input: Division by zero\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;66;03m# Raise a custom error\u001b[39;00m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[0;32m 10\u001b[0m \u001b[38;5;66;03m# Handle another specific exception\u001b[39;00m\n\u001b[0;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mError: Invalid input type. Please enter numbers only.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[1;31mValueError\u001b[0m: Invalid input: Division by zero"
]
}
],
"source": [
"# your code goes here"
"def divide_numbers(a, b):\n",
" try:\n",
" # Code that may raise an error\n",
" result = a / b\n",
" except ZeroDivisionError:\n",
" # Handle specific exception\n",
" print(\"Error: Division by zero is not allowed.\")\n",
" raise ValueError(\"Invalid input: Division by zero\") # Raise a custom error\n",
" except TypeError:\n",
" # Handle another specific exception\n",
" print(\"Error: Invalid input type. Please enter numbers only.\")\n",
" raise ValueError(\"Invalid input: Non-numeric values\") # Raise a custom error\n",
" else:\n",
" # Code to execute if no exceptions occur\n",
" print(f\"Result: {result}\")\n",
" finally:\n",
" # Code to execute regardless of exceptions\n",
" print(\"Division operation completed.\")\n",
"\n",
"# Example usage:\n",
"divide_numbers(10, 2) # Successful division\n",
"divide_numbers(10, 0) # Division by zero error\n",
"divide_numbers(\"ten\", 2) # Invalid input type error"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -66,7 +115,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down