I'm afraid there's no way out of it. All I can say is keep a close eye on your heirs. If you find yourself in a situation with a king that has no heirs and you care about continuing your campaign, then use the .unfreeze. cheat code. This will cause an heir to be born the next year. If your king is old and heirless however, he will probably still die before your king comes of age. You can also try marrying your princesses to your best generals. This increases the chance, that if your royal line fails, there may be a civil war in which the generals with royal blood will be major players.