Exception Handling: Meaning and Semantics
exception han开发者_如何学运维dling: what do they actually do.
consider the scenario that I am reading a file for input and I want to put try/catch the file opening and reading code within a try/catch clause. What happens if there is error in reading the file(file doesnt exists etc). The code goes to the catch block, but what should I put in there.
What is the use of catch block, does the catch block suspends the normal running of the program and the program exists after the catch block?
Thanks in advance for clearing these doubts!!!
Since you didn't provide a specific language, I'm just going to answer as a general concept.
When an exception is hit, the code will jump to the catch block, skipping over all code between the exception and the catch block.
There is no general answer to what you should put in the catch. It depends entirely on your specific case. You will often want to log the error somewhere and then take some appropriate action. What an "appropriate action" is can vary greatly. If the code which is failing is initiated by a user, you would want to return some error message to the user. If it is in some sort of nightly job you may want to send an email alerting someone of the problem, etc.
精彩评论