Most of the time, a bankruptcy filing is the kiss of death for shareholders. Every once in a while, going through bankruptcy actually helps stock investors -- and the rewards can prove very lucrative.

But before you go digging through the debris of the stock market looking for diamonds in the rough, you have to understand just how rare those profitable bankruptcy opportunities are -- and the massive risks involved. Otherwise, you'll probably accomplish nothing but losing your shirt.

Why Dynegy jumped
The latest bankruptcy story with a twist involves Dynegy (NYSE: DYN), a power-generation utility company that's been plagued with problems lately. Earlier this week, several of Dynegy's subsidiaries filed for bankruptcy, with the goal of breaking expensive leases with Public Service Enterprise Group (NYSE: PEG) and reducing the company's overall debt burden. Yet to many people's surprise, Dynegy shares actually rose after Monday night's filing.

The answer to why this happened stems from some unusual circumstances. Ordinarily, it's very difficult for a parent company to do what Dynegy did here, moving assets and liabilities among subsidiaries with the result of sheltering the company's power plants for direct claims by bondholders. Yet while many Dynegy bondholders plan to challenge the company's moves, Dynegy says it already has the support of a substantial fraction of investors holding its bonds.

If the gambit succeeds, then Dynegy's shareholders -- which happen to include activist investor Carl Icahn -- could emerge unscathed even as bondholders take a haircut on their bonds.

The exception that proves the rule
That's unusual. Typically, when companies get to the point at which they need to restructure their debt, shareholders end up with nothing. For instance, that's what happened at General Motors (NYSE: GM), where existing shareholders got nothing and creditors received stock in the post-bankruptcy entity.

But in a select few cases, companies get through bankruptcy without leaving their shareholders entirely destitute. For instance, USG (NYSE: USG) filed for bankruptcy protection in 2001 in order to get a handle on its potential asbestos litigation exposure. After five years in bankruptcy, the company emerged having established a settlement trust fund for claims, and watched its shares jump nearly 20 times in value. Similarly, investors in PG&E (NYSE: PCG) who sat through three years of restructuring came away with a three-bagger.

Perhaps the most interesting recent bankruptcy case was General Growth Properties (NYSE: GGP). Given the environment for retail real estate in early 2009, it certainly seemed natural for the shopping mall operator to succumb to economic reality. But the move was actually just part of a larger strategy to restructure debt, as the company claimed more assets than liabilities in its bankruptcy filing.

General Growth shares traded as low as $0.33 and were delisted from the New York Stock Exchange during the bankruptcy. Eventually, though, numerous investors became interested in the company, and after a battle with Simon Property Group, a group including Brookfield Asset Management (NYSE: BAM) as well as investment firms run by Bruce Berkowitz and Bill Ackma, ended up with huge profits.

Buyer beware
But just as stories from lottery winners shouldn't prompt you to spend your hard-earned dollars buying tickets, the occasional bankruptcy that turned out well for shareholders shouldn't persuade you that bankrupt companies make a good investment. Countless bankruptcies have resulted in complete losses for equity investors, despite their often trading as penny stocks on minor stock exchanges for months or even years while the bankruptcy proceedings go on.

So when you see a company declaring bankruptcy, don't jump into suddenly cheap shares expecting a big windfall. Once in a blue moon, you'll find a situation worth exploring -- but the rest of the time, you'll just be throwing away good money after bad.

"Too big to fail" banks got lots of attention during the financial crisis, thanks to government bailouts that kept most of them from having to declare bankruptcy. Now, the Fool has found two stocks that are too small to fail -- and have the prospect for huge gains. Check them out here in the Fool's latest special report. It's free!