When you face hard times, it's natural to look for a silver lining. For homeowners who have to sell their homes at a loss, however, there's not much good news. In fact, some homeowners are getting a nasty surprise from the IRS.

The tax code draws a distinction between investment property and your personal residence. So when companies in the homebuilding business have to sell their inventory at a loss, they'll have legitimate business losses that they can claim on their corporate tax returns. Indeed, shareholders in companies like Hovnanian (NYSE:HOV), Pulte (NYSE:PHM), and Beazer (NYSE:BZH) can expect to see such losses as the homebuilders shed bloated inventories.

Similarly, if you've got a piece of rental property that you have to sell at a loss, you may be able to claim it on your tax return. In many cases, however, the tax benefits you've been getting while you owned your rental property offset any drop in the property's value. So even if the value of your property has dropped, you may not have a tax loss to claim.

Home sweet home
The home you live in gets special treatment from the IRS. For a long time, the tax code stuck it to homeowners both ways -- you had to pay tax on gains if you made money on your home sale, but you didn't get to claim a loss. When the laws changed to give taxpayers tax-free treatment on up to $500,000 of home-sale gains, it made things a bit fairer.

But that still doesn't help if you're sitting on a money-loser. You'll bear the full brunt of any money you lose as a result of falling home prices, without any benefit from tax deductions. But what you probably won't expect is a big tax bill.

Walking away
Yet a tax bill is exactly what some homeowners will get. With steep price declines in many areas of the country, it's tempting just to walk away from a mortgage if your home isn't worth as much as what you owe. After all, if you sell it, you won't have enough to repay your mortgage.

There are a couple of problems with this. Most importantly, usually, letting your bank foreclose on your property won't stop it from collecting any shortfall from your other assets.

But there's another downside. Even if your bank writes off any shortfall on your mortgage, the IRS still considers that write-off as taxable income to you. In its eyes, the bank bailed you out of part of your loss, so it'll tax you on that bailout money almost as if the bank wrote you a check.

What to do
Unfortunately, there isn't much for homeowners in these unfortunate situations to do about the tax consequences of falling home prices. Some potential solutions, such as trying to convert a personal residence to a business or investment use, aren't very practical for most homeowners affected by the housing downturn.

Being aware of the pitfalls, however, will keep you from having to face any unpleasant surprises. In the end, that may be the best you can do -- until home prices start rising again.

Related articles:

Learn more about ways you can cut your taxes in our Tax Center.

If you're struggling to make ends meet, or if you're just looking for good advice about your finances, take a look at Motley Fool Green Light. The newsletter has plenty of information on buying, owning, or selling your home, along with tips you can use to save hundreds of dollars every month. Browse the service free for 30 days.

Fool contributor Dan Caplinger hopes he isn't underwater on his mortgage yet -- especially because he put 20% down. He doesn't own shares of the companies discussed in this article. The Fool's disclosure policy doesn't have any nasty surprises.