Explanation
๐ธ x = int("abc")
-
This line tries to convert the string "abc" into an integer.
-
But "abc" is not a valid integer, so Python raises a:
๐ธ except ValueError:
-
This catches the ValueError and executes the code inside the except block.
๐ธ print("fail")
-
Since the error was caught, it prints:
✅ Output:
Key Concept:
try-except is used to handle errors gracefully.
int("abc") fails, but the program doesn’t crash because the except block handles the error.


0 Comments:
Post a Comment