How can I avoid getting an error when passing as argument to the function do-http-request an invalid host.
Is there any way that I can catch the error like the Java’s exception-handling mechanism ?
How can I avoid getting an error when passing as argument to the function
Share
Sure, CL has a very nice condition system. One easy option would be wrapping the call to
do-http-requestinignore-errors, which returnsnil(and the condition as a second value) if an error condition was signalled in the wrapped code. You could then check fornilafterwards.If you want something more like exception handling in Java, just use
handler-caseand add an appropriate error clause (I don’t have AllegroServe installed, but I suppose you get asocket-errorfor providing a wrong URL – just change that part if I misread):If you need
finally-like functionality, useunwind-protect:You can even get more fancy, and e.g. use
handler-bindto handle the condition stack upwards by invoking a restart somewhere down the stack, without unwinding it. For example, ifdo-http-requestprovided a restart to try again with another URL, you could handle your error condition by invoking that restart with a new URL to retry. I just mention this for the sake of completeness – it would be overkill for your use case, but being able to resume (possibly expensive) computations easily can be a rather convenient feature.