Explanation:
๐น try:
This block contains code that might raise an error. In this case:
-
You're trying to convert the string "abc" into an integer.
-
But "abc" is not a number, so Python can't convert it.
๐น This raises a:
๐น except ValueError:
When Python sees the ValueError, it skips the rest of the try block and runs the code in the except block:
Final Output:
Summary:
-
Python tries to run int("abc") → fails
ValueError is caught
except block handles it gracefully and prints "Invalid" instead of crashing


0 Comments:
Post a Comment