Skip to content

Commit 4072ec9

Browse files
committed
Created using Colab
1 parent e0e1cf6 commit 4072ec9

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

Exception_Handling.ipynb

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyP3FgGKcqtv8VFwecvF8QRW",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/ambilisunil/python_basics/blob/main/Exception_Handling.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"source": [
32+
"# Exception handling in is done using the **try-except** block to handle runtime errors gracefully without crashing the program."
33+
],
34+
"metadata": {
35+
"id": "WfLADh7i3YTL"
36+
}
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"source": [
41+
"**except**"
42+
],
43+
"metadata": {
44+
"id": "Ad5HiYCD4AVM"
45+
}
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 5,
50+
"metadata": {
51+
"colab": {
52+
"base_uri": "https://localhost:8080/"
53+
},
54+
"id": "OLSqLsS13T2W",
55+
"outputId": "190623cf-f9d2-4dd7-c2cc-6c08566dd80b"
56+
},
57+
"outputs": [
58+
{
59+
"output_type": "stream",
60+
"name": "stdout",
61+
"text": [
62+
"Enter a number: 0\n",
63+
"Error: Cannot divide by zero!\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"try:\n",
69+
" num = int(input(\"Enter a number: \"))\n",
70+
" result = 10 / num\n",
71+
"except ZeroDivisionError:\n",
72+
" print(\"Error: Cannot divide by zero!\")\n",
73+
"else:\n",
74+
" print(f\"Result: {result}\") # Runs only if no error occurs\n"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"source": [
80+
"**finally** The finally block runs no matter what (even if an exception occurs).\n"
81+
],
82+
"metadata": {
83+
"id": "G3wFsx_O4Cz-"
84+
}
85+
},
86+
{
87+
"cell_type": "code",
88+
"source": [
89+
"try:\n",
90+
" file = open(\"example.txt\", \"r\")\n",
91+
" data = file.read()\n",
92+
"except FileNotFoundError:\n",
93+
" print(\"Error: File not found!\")\n",
94+
"finally:\n",
95+
" print(\"Closing file...\")\n"
96+
],
97+
"metadata": {
98+
"colab": {
99+
"base_uri": "https://localhost:8080/"
100+
},
101+
"id": "dtg-gL6w4Eqf",
102+
"outputId": "ce4e6392-0a7e-454d-adc1-027364bd7951"
103+
},
104+
"execution_count": 7,
105+
"outputs": [
106+
{
107+
"output_type": "stream",
108+
"name": "stdout",
109+
"text": [
110+
"Error: File not found!\n",
111+
"Closing file...\n"
112+
]
113+
}
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"source": [
119+
"**rise** like throw"
120+
],
121+
"metadata": {
122+
"id": "jYcwchCg4eQZ"
123+
}
124+
},
125+
{
126+
"cell_type": "code",
127+
"source": [
128+
"def check_age(age):\n",
129+
" if age < 18:\n",
130+
" raise ValueError(\"Age must be 18 or above.\")\n",
131+
" return \"Access granted!\"\n",
132+
"\n",
133+
"try:\n",
134+
" print(check_age(16))\n",
135+
"except ValueError as e:\n",
136+
" print(f\"Error: {e}\")\n"
137+
],
138+
"metadata": {
139+
"colab": {
140+
"base_uri": "https://localhost:8080/"
141+
},
142+
"id": "gtJxlsyA4fs2",
143+
"outputId": "5d172e2b-3c30-4dd0-ceab-e08d744cbfa0"
144+
},
145+
"execution_count": 9,
146+
"outputs": [
147+
{
148+
"output_type": "stream",
149+
"name": "stdout",
150+
"text": [
151+
"Error: Age must be 18 or above.\n"
152+
]
153+
}
154+
]
155+
}
156+
]
157+
}

0 commit comments

Comments
 (0)