You're in a room.
There's a guy in there that's about to brutally rape a woman, strangle her and then he's going to chop her up into little bits and feed her remains to a hundred children.
The only way you can stop him is to kill him. There's no other way. You've tried everything you know to prevent it but, an innocent person is going to be brutally tortured and murdered and even more damaging atrocities are going to come about that could effect children for the rest of their lives! Something must be done and you're the only one who can do it! He's right in front of you, there's no time to do anything other than act!
So, you kill him.
Then, a bunch of guys take you away, force some medication into you and you find out that none of what you thought was true was, in fact, true. It was all a terrible hallucination caused by your mental illness. You killed an innocent man because you were insane. Your "reality" simply wasn't real.
Welcome to the gas chamber? I don't think so.
This could actually be a scenario for a mentally ill person. I've seen plenty of them become stabilized on their medication and sincerely and honestly apologize for things they may have done when they were acting out their irrational and insane behaviors. It's common for them to understand and be shocked by their own behavior once they're stabilized. It must be a terribly traumatic experience and I thank the powers that be that I appear to be sane.. mostly.
No, such solutions don't console the families of the victims. But, Law is supposed to dispense social justice, not simply meet out unthinking revenge.