Saving for retirement requires discipline. Ideally, you are able to amass a nice nest egg by putting aside a big chunk of money each month and investing it wisely. But it doesn’t take much for you to get off track. Life happens, and you are often faced with expenses and other burdens that make saving for retirement less of a priority.

But you needn’t let life get in the way of your long-term financial goals. With some planning and good decision making, you can continue to save regardless of what comes your way.

Here are some things that can throw your retirement planning out of whack, and how to deal with them.

1. Job loss

If you suddenly find yourself out of work, it can be a huge blow to your retirement savings because you no longer have income you can set aside. You are no longer able to contribute to that company’s 401(k) plan, and you lose any matching or direct contributions that were coming from your employer.

An extended job loss could result in the loss of tens of thousands of dollars in retirement savings over time. However, you can always contribute more to your 401(k) plan later to catch up once you get back to working, and if you have a large enough emergency fund (at least three to six months’ worth of income), you may still be able to contribute to retirement through individual retirement accounts (IRAs) or taxable brokerage accounts.

A job loss can hurt, but if you are relatively young and have a lot saved already, the loss of a few months of retirement contributions won’t make too much of a difference in the long run.

2. Divorce

Dissolving a marriage is a tough choice to make, and it’s one that can be fraught with financial implications. There could be large upfront legal costs associated with the divorce itself, and the long-term impact can be significant. Suddenly, your household income has been split in half while you each now have to pay for your own separate housing costs, utilities, and meals. You may be on the hook for child support or alimony payments. And now the combined retirement nest egg you were counting on has been slashed. When this happens, it’s hard to think about retirement savings.

You can protect your retirement, however, by avoiding the temptation to cash out accounts upon your divorce. Some couples have unfortunately been known to do this during the asset-splitting process, resulting in huge capital gains taxes and penalties (plus the loss of any potential earnings from that money).

If you and your spouse are considering divorce, be sure to think hard about the financial implications. If you do decide to separate, don’t lose sight of the importance of saving for retirement, despite the new challenge in doing so. (See also: How to Protect Yourself Financially During a Divorce or Separation)

3. A major emergency

You suffered a significant injury and only part of your medical bills were covered by insurance. You totaled your car. Your house was gutted by fire. When these things happen, retirement saving may be the last thing on your mind. In fact, if you are not prepared, you may end up raiding your retirement funds to pay your bills. This could mean penalties and taxes and years of potential lost income.

You can continue to save for retirement, however, if you’ve built up a significant emergency fund. Three to six months’ worth of income is a good rule of thumb to cover whatever unexpected costs you may have. With a good emergency…