While in many cases this error may indeed be caused by sand interfering with the extension mechanism etc. and you should try solutions suggested in the other answers here, it may also happen that your camera has a manufacturing defect.
While I was travelling in the Middle East, the lens of my Canon PowerShot S100 got stuck and I received the message: "Lens Error, Will shut down automatically, Restart camera". I contacted Canon and received the following answer:
It has been confirmed that some PowerShot S100 digital cameras
encounter a lens error caused by a disconnected part inside the
In particular, this lens error may occur when the camera is
used in certain environmental conditions such as high temperature
The error is affective for cameras with the serial
number starting with the number “29” through “41”. For example:
29xxxxxxxxx , 32xxxxxxxxxx or 41xxxxxxxx.
I sent it to a local Canon representative for warranty service, and received it back a couple of weeks later, fixed.