I know that the global variable $! holds the most recent exception object, but I am having confusion with the syntax below. Can anyone help me understand the following syntax?
rescue $!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This construct prevents exception from stopping your program and bubbling up the stack trace. It also returns that exception as a value, which can be useful.
After this line,
awill hold either requested data or an exception. You can then analyze that exception and act accordingly.More realistic example
You either get the lines or the error (if file doesn’t exist, you don’t have permissions, etc). In any case, execution doesn’t stop.