CBSE Class 12 Computer Science
Question 24 of 30
Exception Handling — Question 10
Back to all questionsval = input("Enter a number: ")
try:
pval = int(val)
except ValueError:
print("Error: Conversion to integer failed.")Enter a number: 23
Enter a number: python
Error: Conversion to integer failed.
In this modified code the input function prompts the user to enter a number. The try block attempts to convert the input value to an integer using int(val). If the conversion is successful, pval will hold the integer value. If the conversion raises a ValueError (e.g., if the input is not a valid integer), the program jumps to the except block and prints an error message indicating that the conversion to an integer failed.