Did you go through a foreclosure or obtain a loan modification in the past year? Do you expect to do so in the near future? You could wind up with an unexpected and unwelcome tax bill.

As you may know, Congress passed a law that temporarily exempts homeowners, through 2012, from having to report mortgage debt written off by a lender as income for tax purposes. However, if you took a big chunk of cash out through a home equity loan, you might not qualify for the exception.

Here's how it works. The IRS typically considers mortgage debt forgiven by a lender to be income for tax purposes. So if you owed $250,000 on your mortgage when you lost your home to foreclosure, but the bank was only able to recover $150,000 by selling the property at auction, the $100,000 it couldn't collect could be charged to you as income.

In other words, the IRS normally considers that the bank is giving you $100,000 by writing off the debt obligation you couldn't cover. So you'd normally have to count it as income. However, Congress provided an temporary exception through 2012 with the Mortgage Forgiveness Debt Relief Act of 2007.

The same thing applies to loan modifications that involve writing down principal. If you owe $250,000 on your mortgage and the bank reduces what you owe to $200,000 as part of a loan modification, you have to report that $50,000 as income on your federal taxes. Again, the temporary exception applies - but not always.

No exception for debt not spent on home

Here's the problem. Under the Mortgage Forgiveness Debt Relief Act, you can't claim the exception for debt that resulted from a home equity loan or home equity line of credit if you didn't use the money to make improvements to the house. Money spent on repairs or home improvements is ok, since that's considered to contribute to the value of your principal residence.

Unfortunately, during the housing boom, a lot of Americans took out home equity loans to pay for cars, vacations, second homes and a generally higher standard of living. Now many of those people are the same ones seeking loan modifications or going into foreclosure - and they're still on the hook to the IRS for the debt.

Consider. You originally paid $250,000 for your home, but it increased in value to $350,000 during the housing bubble. You took out a home equity loans that increased your indebtedness to $350,000 and spent the money things other than home repairs or improvements.

You then lost the home to foreclosure, and the bank was only able to sell it for $150,000 at auction. Result: The bank or banks (if you got your home equity loan from a different lender) write off $200,000 of your debt, but you're on the hook to the IRS for $100,000 of it (the amount that exceeds what your originally paid for the home).

Also applies to loan modifications

Same with a loan modification. Suppose that, instead of going to foreclosure, you were able to obtain a loan modification in which the bank wrote off $50,000 of your principal to reflect the diminished value of your home. You'd have to report, and pay taxes on, that $50,000.

This applies even if you used the money from a home equity loan to pay off student loans or other debt, such as a car loan or credit cards, as many financial advisers were recommending a few years back. You're exempt only if you used the money to make home repairs or improvements that contributed to the value of your home.

A few other wrinkles. You're also on the hook for debt forgiven in a foreclosure or loan modification on a second home or investment property - only your primary residence qualifies. You may still be able to claim an exception for a second home if it was your primary residence for two of the past five years, though.

May qualify if insolvent

Also, you may not have a tax liability for any forgiven real estate debt if you are insolvent. This may very well apply to you if you've been through a foreclosure, but is less likely to apply if you've simply had a loan modification.

There's been a lot of discussion in recent months about homeowners embracing "voluntary foreclosure" as a way to get out of an underwater mortgage. However, as the examples above show, simply walking away from your mortgage may not be the simple solution it's made out to be. And obtaining a loan modification may bring with it new financial challenges of its own. If you think you might be facing either situation in the coming year, it's a good idea to consult with a tax advisor before getting in too deeply.