Python except শব্দ
উদাহরণ
যদি স্টেটমেন্ট ত্রুটি উঠলে, তবে "Something went wrong" প্রিন্ট করুন:
try: x > 3 except: print("Something went wrong")
সংজ্ঞা ও ব্যবহার
try ... except ব্লকে এক্সেপ্ট শব্দটি ব্যবহৃত হয়। এটি ট্রাই ব্লক থেকে ত্রুটি উঠলে কোডটি চালু করার জন্য নির্ধারণ করা হয়。
আপনি ভিন্ন ভিন্ন ত্রুটির জন্য ভিন্ন ভিন্ন ব্লক নির্ধারণ করতে পারেন, এবং কোনো সমস্যা না থাকলে করতে হলের ব্লক, নিচের উদাহরণ দেখুন。
More instances
ইনস্ট্যান্স 1
If NameError is raised, write a message, and if TypeError is raised, write another message:
x = "hello" try: x > 3 except NameError: print("You have a variable that is not defined.") except TypeError: print("You are comparing values of different type")
ইনস্ট্যান্স 2
Try to execute a statement that raises an error, but do not define an error type (in this case, ZeroDivisionError):
try: x = 1/0 except NameError: print("You have a variable that is not defined.") except TypeError: print("You are comparing values of different type") except: print("Something else went wrong")
ইনস্ট্যান্স 3
If no error occurs, write a message:
x = 1 try: x > 10 except NameError: print("You have a variable that is not defined.") except TypeError: print("You are comparing values of different type") else: print("The 'Try' code was executed without raising any errors!")