system
Given the following post, respond as if you're commenting on Reddit:
human
[Batman] We can talk about the philosophical reasons why Batman doesn’t kill the Joker, but why has the Joker never been legally executed?

With all the crimes he’s committed, there’s no way a mental illness defense would still exonerate him. Besides, it’s not like a trial is really necessary, the man himself glories in his crimes. Certainly Batman would be at least indifferent to this option, as he’s discharged his duty by bringing Joker in. Now it’s up to the law to decide what to do with him. So why hasn’t the law decided to execute him? Why do they keep trying to send him to Arkham, where they know he’ll just escape again?
gpt
Because Gotham isn't located in Texas. :)  More seriously, there are several different reasons.

First, in real life the US experienced a huge rise in violent crime from the late 1970s through the mid-1990s.  During this crime spike, there was a stereotype of the court system just letting violent offenders go free.  So the stereotype goes, judges would fall for anything a slick defense attorney said, and some quack psychiatrist could get vicious murderers sent to cushy hospitals instead of prison.  Jails were said to have a revolving door, and courts were seen as ineffective (whether this was true or not, the *perception* certainly existed).  This resulted in a "tough on crime" attitude from many politicians, new laws with harsher sentences were imposed, and the US prison population exploded.  Eventually, crime rates dropped off again (right now, violent crime rates are about where they were in the late 50s and early 60s) and in the last 5-10 years, we've seen a general trend where some of those laws are being repealed.

Gotham sort of perpetually exists in this specific time period, where crime is skyrocketing and courts just fumble around.  Police are incompetent, judges let violent criminals go with just a slap on the wrist, and psychiatrists think that every person in a weird costume is a soul in need of treatment.  This is explicitly stated in Frank Miller's The Dark Knight Returns story (where a psychiatrist says that *society* is to blame for the Joker's crimes).

There's also probably a bit of a "rolling timeline" thing going on.  In real life the Joker has likely appeared in thousands of issues over the 80+ years the character has been around.  But in-universe, let's say he's only been around for the last 7 or 8 years and has probably only had a dozen or so criminal escapades.  Some of those resulted in dead people, but some of them involve Joker robbing a casino armed with a lemon meringue pie cannon (or some other dumb thing).

So instead of a body count of thousands, let's say Joker has killed maybe 50 people total, and half of them you can't really prove he's responsible for (and several of the dead are mobsters and criminals).  His schemes range from deadly to zany, but really only his most recent one had a significant number of dead people.  He had a few bank robberies, he had a couple of poisonings, and then there was that time he seized a toy company and forced them to manufacture "talking Joker" dolls because he wanted to deliver them to the children of Gotham for Christmas.  He got stuck in a chimney and Batman had to call the fire department to pull him out.  But in the last year or so, Joker seemed to get a lot darker.  He set off a gas canister in a store, and 15 people died with a Joker-grin on their faces.  Batman beat the dog shit out of him for that one, and Gotham's DA talked about seeking the death penalty for it.  But Joker already has a very public record of being committed to the insane asylum, and people suspect that's what will happen again.