Taking an early withdrawal from your 401(k) is not only costly in the short term, it can also jeopardize your long-term retirement goals.

The bottom line: Withdrawing money early from your retirement accounts carries heavy financial consequences, but sometimes the benefit outweighs the cost of taking out a 401(k) loan. Just go into it with open eyes. And when you are through, make a new financial plan that will protect you from facing this kind of difficult and costly decision again in the future.

There are a few situations when you are in a financial pinch and you won't get penalized for taking money out of your 401(k) or other retirement savings accounts.

For example, you can take penalty-free distributions from qualified plans due to a total or permanent disability. Minor or partial disabilities don't qualify. According to the IRS, you are considered disabled if:

•You can provide proof that you cannot do any substantial gainful activity because of your physical or mental condition.

•A physician determines that your condition can be expected to result in death or to be of long, continued and indefinite duration.

Some experts recommend first applying for state disability insurance to make it easier to prove your status to the retirement plan administrator. To take a 401(k) hardship withdrawal, you must fill out IRS Form 5329 to get out of paying the penalty and to ensure that you are adhering to IRS 401(k) loan rules.

Another exception to the rule is if you are drowning in medical debt. You can withdraw from your retirement accounts to cover unreimbursed, out-of-pocket medical expenses that exceed 10 percent of your adjusted gross income. These expenses must be paid in the same year you take the distribution and the distribution is not subject to penalty of tax if withdrawn from an IRA.

The difference between these expenses and 10 percent of your adjusted gross income is the amount eligible for this exception. For example, if your adjusted gross income is $60,000 and your unreimbursed medical expenses are $9,000, the maximum amount that you can distribute without penalty is calculated as 9,000 – (60,000 x 0.10) = $3,000.

Also, if you are getting divorced, you might be required by the court to divide the funds with your former spouse or a dependent. These distributions are usually ordered under a property settlement under a qualifying domestic relations order and are exempt from an IRA or 401(k) withdrawal penalty.

Morgan Quinn writes for GOBankingRates.com.