Here is an example program that demonstrates the use of try and catch block to handle an exception:
In this program, we define a function divide that takes two integers as arguments and throws an exception if the second argument is zero. The main function reads two integers from the user and calls the divide function within a try block. If an exception is thrown, the catch block catches the exception and prints an error message.
Sample Output:
In this case, the user tried to divide 10 by 0, which is not allowed, so the program threw an exception and the catch block caught it and displayed the error message.
No comments:
Post a Comment
If you have any doubts, please let me know