-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_handling.py
More file actions
144 lines (96 loc) · 4.25 KB
/
exception_handling.py
File metadata and controls
144 lines (96 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# --------- # Error and Exception Handling # -------- #
# Errors Kya Hain? (Types of Errors)
'''
A. Syntax Error (Likhne ki galti)
Jab aap Python ke rules todte hain (jaise bracket bhool jana ya spelling galat likhna). Ye code chalne se pehle hi aa jata hai.
Example: if x = 5: (Yahan == hona chahiye tha).
B. Logical Error (Sochne ki galti)
Code toh sahi chalta hai, par jawab galat aata hai.
Example: Aapko 2 numbers plus karne the, par aapne galti se minus - likh diya.
c. Indentation Error (spacing m koi msla hojana)
D. Runtime Error / Exceptions (Chalte waqt ki galti)
Code sahi likha hai, lekin chalte waqt kuch aisa hua jo computer nahi kar saka. Ise hi hum Exceptions kehte hain aur ise hi humein handle karna hota hai.
'''
# Exception Error Kab aata hai?
'''
ZeroDivisionError,
Jab aap kisi number ko 0 se divide karte hain.
ValueError,
Jab aap galat tarah ka data dete hain (jaise number ki jagah text).
FileNotFoundError,
Jab aap aisi file kholte hain jo computer mein hai hi nahi.
IndexError,
Jab aap list se aisa item mangte hain jo wahan hai hi nahi (e.g., list mein 3 items hain aur aap 5th mang rahe hon).
'''
# Exception Handling
'''
Keyword Purpose
1. try → Wrap the block of code that might cause an exception.
2. except → Handle the exception if it occurs
3. else → Run code only if no exception occurs
4. finally → Run code no matter what, whether there’s an exception or not
5. raise → Manually throw an exception
So these are the keywords that we use and all these
keywords has their separate purpose as mentioned. ̄
'''
"""Question: User se do numbers lekar divide karna aur har galti ko sambhalna."""
# try:
# a = int(input("pehla number likho:"))
# b = int(input("second numberlikho:"))
# try akele nhi likh sakte , except situation btani hogi k ye error agr ajae tw ye text likha hoa ae
# result = a / b
# print(result)
# except ZeroDivisionError:
# # #Except: Agar zero se divide kiya toh ye chalega.
# print("Aap zero (0) se divide nahi kar sakte!")
# except ValueError:
# # #Except: Agar user ne number ki jagah 'abc' likh diya.
# print("Yaha srf numbers add kiye ja skte hai")
# except Exception as err:
# print(f"sorry there is an err as {err}")
# else:
# print("Good there is no exception")
# finally:
# print("i will run no matter what!")
"""raise in error handling, ap khud se consider krrhe ho k ye error askata hai tw ye print krwadena. """
'''age = int(input("Tell your name:"))
try:
if age < 10 or age > 18:
raise ValueError("your age must be between 10 to 18")
else:
print("Welcome to the club")
except Exception as err:
print(f"an error as occured as {err}")
print("the club will start soon")
'''
# ----------------- # File Handling # ----------------- #
'''Ab File Handling ko Detail mein samjhte hain:
File handling ka matlab hai computer ki kisi file mein data dalna ya wahan se data nikalna. Iske 3 main steps hote hain:
Open: File ko kholna.
Work: Usmein likhna (write) ya parhna (read).
Close: File ko band karna (taaki memory saaf rahe).
Modes jo aapko yaad rakhne hain:
'r' (Read): Sirf parhne ke liye. (Agar file nahi hai toh error aayega).
'w' (Write): Naya likhne ke liye. (Ye purana sab mita deta hai!).
'a' (Append): Purane data ke aage naya jorne ke liye.'''
# 1. File mein likhna (Write)
# with open(r"C:\Users\hp\Desktop\New folder\python-learning-journey\oop_concept.py", "w") as f:
# f.write("Ye meri pehli file hai.")
# f.write("Python seekhna aasaan hai.")
# 2. File ko read krna (Read)
#
# with open(r"C:\Users\hp\Desktop\New folder\python-learning-journey\setsNdictionary.txt","r") as f:
# data = f.read()
# print(data)
# 3. File ko append krna
# "Append" ka matlab hota hai kisi cheez ke aakhir mein kuch aur jorna.
# Kab use karein? Jab aap chahti hain ki purana data bhi rahe aur naya data uske niche se shuru ho.
# Fayda: Ye "w" mode ki tarah purana data delete nahi karta.
'''
with open("setsNdictionary.txt","a") as f:
f.write("\n practicing file handling") # Ye purane data ke niche Ahmed likh dega'''
# r lagana mat bhooliye ga
# path = r"C:\Users\hp\Desktop\New folder\python-learning-journey\setsNdictionary.txt"
# with open(path, "a") as f:
# f.write("\n practicing file handling")
# print("Mubarak ho! Data append ho gaya.")