Exceptions · חריגות
When things go wrong
- A runtime error normally crashes the program.
- An exception is Python's signal that something went wrong.
- Exception handling lets you catch it and keep running.
כאשר דברים הופכים לשגוי
- חריגה היא האות של Python שSomething הלך לא נכון.
- חיץ (Exception) הוא האות שפייתון שSomething השתבש.
- טיפול בחריגות מאפשר לך לתפוס אותן ולהמשיך בביצוע.
try / except
- Put risky code in a
try:block. - If it raises an exception, the
except:block runs instead of crashing. - If nothing goes wrong, the
exceptblock is skipped.
ניסיון / שגיאה
- הנח קוד מסוכן בתוך בלוק
try:. - אם הוא מייצר חריגה, בלוק ה-
except:יתבצע במקום התמוטטות. - אם אין בעיה, בלוק ה-
exceptידולג.
try:
number = int("not a number")
except ValueError:
number = 0
print(number)
Catch the right error
- Name the type of error you expect:
except ValueError:. ValueErroris bad text for a number;ZeroDivisionErroris divide by zero.- Catching a specific type means you do not hide other bugs.
תפיסת השגיאה הנכונה
- ציין את סוג השגיאה שמצפה לה:
except ValueError:. ValueErrorהיא טקסט רע עבור מספר;ZeroDivisionErrorהיא חלוקה באפס.- תפיסת סוג ספציפי מונעת הסתרת באגים אחרים.
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")
finally and raise
- A
finally:block always runs, error or not — good for cleanup. raiselets you signal an error on purpose.raise ValueError("message")stops the code unless something catches it.
לבסוף והרמה
- בלוק ה-
finally:תמיד מתבצע, עם או בלי שגיאה — מצוין לניקוי. raiseמאפשר לך להפעיל שגיבה במטרה.raise ValueError("message")עצור את הקוד אלא אם כן מישהו תופס אותו.
try:
raise ValueError("bad value")
except ValueError as e:
print("Caught:", e)
finally:
print("Always runs")
Why it matters
- A robust program should not crash on bad input.
- Handling exceptions lets it warn the user and carry on.
- It also makes sure files and other resources are closed cleanly.
מדוע זה חשוב
- תוכנה עמידה לא אמורה להתמוטט על קלט פגום.
- טיפול בחריגות מאפשר לה להציג אזהרה למשתמש ולהמשיך.
- היא גם מבטיחה שקבצים ומשאבים אחרים ייסגרו בצורה נקייה.
Common mistakes
- Catch a specific error (
except ValueError:), not a bareexcept:. - Put only the risky line inside the
try.
טעויות נפוצות
- תפסו שגיאה ספציפית (
except ValueError:), לא חיץ עירוםexcept:. - הכנס רק את השורה המסוכנת לתוך ה-
try.
Now you try
- Use
try/except(andraisein the last task). - Press Check answer to test your code.
כעת תנסו בעצמכם
- השתמש ב-
try/except(וב-raiseבמשימה האחרונה). - לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
try / except
Code runs in try; if it fails, control jumps to except. · הקוד רץ ב-try; אם הוא נכשל, הבקרה עוברת ל-except.
Write safe_int(text) that returns int(text). If the text is not a valid number, catch the ValueError and return -1 instead. · כתוב safe_int(text) ש-מחזיר int(text). אם הטקסט אינו מספר תקף, תפוס את הValueError והחזר -1 במקום זאת.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write divide(a, b) that returns a / b. If b is 0, catch the ZeroDivisionError and return None instead. · כתוב divide(a, b) ש-מחזיר a / b. אם b הוא 0, תפוס את הZeroDivisionError והחזר None במקום זאת.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write withdraw(balance, amount). If amount is more than balance, raise ValueError. Otherwise return the new balance (balance - amount). · כתוב withdraw(balance, amount). אם amount גדול מ-balance, raise ValueError. אחרת החזר את המאזן החדש (balance - amount).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.