"Don't ever touch your retirement savings!"

Thus says the internet. Think of it as the third rail of retirement advice. And, to be clear, the people who are saying it are right in almost all cases.

After all, we're already woefully behind on our retirement savings – according to a recent study by the Transamerica Center for Retirement Studies, the median Millennial household only has $37,000 in retirement savings, and the median Baby Boomer household only has $164,000. Raiding your 401(k) fund has the effect of making that situation worse.

Nonetheless, every now and then, a situation crops up where taking out a 401(k) loan may be the "least worst" option. Here are two.

Lots of high-interest debt

Of the roughly 29% of Americans who report having taken some kind of early withdrawal or loan from their retirement plan, over half report having done so to pay off credit card debt. The average credit card charges a roughly 12.5% annual interest rate, and the average American household has $7,136 in credit card debt.

Blackboard with a red eraser on it. "DEBT" is written in white letters and has been partially erased.

Image source: Getty Images.

The average 401(k) loan, by contrast, usually charges an interest rate a percentage point or two above the prime rate -- currently at 4.5%, so assume 6% or so. Even though withdrawing the money means you miss out on the potential compounding of that money in the stock market -- historically speaking, between 6% and 7% after inflation -- you can see that it's basically a wash between slowly chipping away at a credit card at 12.5% APR versus paying it off in one fell swoop with a 401(k) loan. Plus, the interest you pay on your 401(k) loan is deposited into your 401(k) -- so at least those interest payments are going to you and not the bank.

Paying off credit card debt with a 401(k) loan still isn't a good option, but if you're caught in a spiral of high-interest credit card debt that you'll struggle to pay off (perhaps a scenario in which you have twice the average household credit card debt), a 401(k) loan could give you the ability to do a needed reset if you aren't able to take advantage of other, better debt consolidation strategies. You would still have to pay all of the money back with after-tax dollars (meaning that you'd lose out on one of the key benefits to the 401(k) -- its tax-deferred status), and if you exit your job, you may be forced to pay the entire balance back immediately. Taking out a loan on your 401(k) isn't an attractive strategy, but it may make sense if you're in a tough spot.

Emergency meltdown

I didn't write "last resort" for the circumstances above, because that phrasing should be reserved for the so-called "hardship withdrawal" option. The IRS specifies the circumstances under which a 401(k) provider may (but is not required to) allow for hardship withdrawals by noting that they could be allowed when there is an "immediate and heavy financial need of the employee" -- and the amount withdrawn "must be necessary to satisfy the financial need." Put a different way, you have to need money quickly, and there must be no other way you can get that money.

To clarify just how up against a wall you have to be to qualify, the IRS further stipulates that you must have already tapped "all other currently available distributions and loans under the plan and all other plans maintained by the employer" -- meaning you have to have already taken out the maximum possible 401(k) loan (which is the lesser of 50% of the vested balance or $50,000).

This should be the true last resort, and not just because it's required by tax law. The money you withdraw for financial hardship is fully taxed and may be subject to a 10% early withdrawal penalty. The money may also not be paid back to your 401(k) (unlike in the 401(k) loan example I noted above), and you will be barred from contributing to the 401(k) plan for six months. Your 401(k) balance will permanently shrink, and you'll be barred from doing anything to grow it for six months.

Making the best of a bad situation

The fact that 29% of Americans report having taken some kind of early withdrawal or loan from their 401(k) is just another sign of how close to the edge too many people are living. It's pretty clear that most Americans aren't tapping their 401(k) for fun. It's because they're in emergency situations, strapped for cash, and starved for options.

So if you're reading this article because you're in dire financial straits like I've described above, consider taking that loan -- and if you qualify for the hardship withdrawal, you probably have no choice but to go for it. When times get better, try to build up an emergency fund that will allow you to handle most financial shocks without major consequences -- think at least six months of your nonnegotiable expenses. That's the only way to get into a sufficiently stable spot that you (hopefully) won't ever have to raid your retirement savings again.