Forecasting demand 5 years into the future is intrinsically highly unreliable. It doesn’t matter if it is capitalism or a command economy. The bet is always going to be risky and someone will have to pay for that risk.
At least with capitalism you have many different people with different perspectives on the risk making independent bets. That mitigates the more extreme negative outcomes.
To add to that, investors who do make the bet get punished for over-building, which is better than tax payers paying for it. And before someone says it, big corps do get bailed out by gov't, but that's definitely goes against capitalist ideas.