401k © Alex Belomlinsky, Photodisc, Getty Images

Raiding your 401k or IRA funds may seem like financial suicide, but in some cases, doing so can makes sense.

Some financial advisers consider it the ultimate act of financial insanity, but if you have already tapped out your emergency cash fund, borrowing from either retirement account can be a viable option.

The number of people borrowing from their 401k retirement accounts has increased recently, according to reports by Wells Fargo and Fidelity Investments, the largest provider of 401k plans.

Borrowing from your company-sponsored 401k plan is akin to taking out a short-term loan.

While you may be able to avoid penalties and taxes, such loans do require you to pay interest. In most cases, you are allowed to borrow half of your 401k balance or up to $50,000, said Andrew Valentine Pool, a portfolio manager for Regatta Research & Money Management, based in New Orleans.

The loan repayment period varies with each company, but it is generally five years or less, he said.

"The majority of people borrow from their 401k for the wrong reasons," said Scott Sonnier, the president and chief investment officer of Financial Management Services of America, based in Lafayette, La. "If there is a hardship or an emergency, then we allow for it. We don't want people accessing their money for vacation or a social function."

Most loans are paid back though payroll deductions, but many require the employee to pay an initial fee in addition to interest, he said. Some loans also require a monthly fee.

Employers require that their employees pay the entire loan back within 30 or 60 days if they leave the position for any reason, said Pool.

"That is a big pitfall," he said.

Individuals who have not paid back a loan before they leave the company are taxed on the remainder of the loan and pay a 10% penalty for early withdrawal, said Sonnier.

Raiding the account also means that employees are not taking advantage of investing their money tax-free or seeing any growth from it, Pool said.

Taking out a loan is viable only if you can pay back the amount quickly. If you need cash to pay for closing costs for refinancing a mortgage and are disciplined enough to allocate the savings for the monthly loan repayments, then it can be a good choice, he said.

The worst option is to borrow from your retirement account to buy something that depreciates, such as a car, or to make speculative investments, such as individual stocks. You should already have money set aside for these.

"Borrowing from your retirement in the short term is not a good idea," he said.

Tapping into your traditional or Roth IRA is another option. The IRS allows you to borrow from either type of IRA once a year without any penalties or fees if you pay back the loan within 60 days.

This is basically an interest-free loan, but you have to be careful to replace the cash within the 60-day period. Missing the deadline means you are subject to federal income taxes, a 10% penalty if you are under 59 years old and potentially even state income tax. It is best to repay the amount a day or two early. The countdown begins the day after you receive the funds. If you are not sure you can meet the window, financial advisors suggest that you skip this option.

If you miss the deadline, the penalties and fees are stiff, depending on the tax bracket you fall in. For instance, the tax rate of a married couple whose gross income is from $72,000 to $146,000 and files jointly is 25%. The couple would also be forced to pay the 10% penalty, as well as state income tax.

"It makes sense as a last resort," said Pool.

Borrowing from your 401k is a better option than taking a withdrawal from your IRA accounts, he said.

If you are purchasing your first home, need money to pay medical bills or funds to pay for your education, you can take a hardship withdrawal.

For your first home, you can borrow the amount of your contributions from your Roth IRA without paying penalties and taxes. The maximum amount is $10,000.

Using a traditional IRA for a down payment is not as beneficial, because you must pay taxes on all withdrawals.

There is no limit on how much you can borrow for medical expenses or higher education and there is also no time limit on the repayment period.

"These three options are manageable, because you are making an investment in yourself," said Sonnier. "However, borrowing from an IRA now will make a dramatic impact on your final value when you retire."

Utilizing your emergency cash fund remains the best option, he said.

"If you have to borrow the money, because you are unemployed, then borrow only the minimum amount to pay your expenses," Sonnier said. "If you take out extra money, you will likely never pay it back. It is your money, so do everything to get educated about it. Know your choices and facts."

Click here to become a fan of MSN Money on Facebook

More from MainStreet: