I've had a few gear cables snap. Mrs Uncle Phil had one break in the Campag shifter on her commuting bike the other day, and it broke about a centimetre away from the nipple inside the shifter. (She rode home in the high gear at the back and just used the front changer to give her 3 gears).
In my toolkit, I generally carry a spare gear cable. And I've had to fit one only only twice, I think, both times on tour.
When the cable's fraying and about to go, you'll notice it stretching, or peculiar gear-changing in the changes before it finally goes. That's the time to fix it - if you're in time before it goes.