Why is castling through and out of check prohibited? Why is castling disallowed when the king is in check or when the square the rook will be placed on is under attack?

What is the rationale behind this ruling?
One rationale that I can think of for not allowing the king to move through check parallels that behind the possibility of capturing en passant after a pawn makes a two-square advance.


The typical pawn move is just a single square forward, and the possibility of advancing two squares on a pawn's first move was a relatively late addition to the game in historical terms. Since the "nature" of a pawn had long been to make a single square advance, when that new sort of pawn move came into being, the en passant capture was a natural counterpart: the pawn doesn't "jump" two squares ahead, but rather "marches" through the intermediate square, and an opposing pawn is thus allowed to capture en passant on the following move if it is in position to have captured the advancing pawn at the intermediate square.
With that explanation in mind, let's consider your rule. Like pawns, a king's typical, natural movement is only a single square in distance. And like the pawn's two-square advance, castling is a relatively new addition to the rules of chess that is intended to speed the game up. So just as the en passant capture reinforces the pawn's nature as a one-square mover, the rule prohibiting a king to castle through a checked square reinforces the king's nature as a one-square mover. He is thought of not as gliding over the checked square, but rather trudging through it. And just as an opponent's pawn has a chance to capture en passant, an opponent's piece guarding the intermediate castling square would be given the right to capture the king. Since that's not a legal part of chess, castling through check in the first place isn't either.


As for why castling is disallowed just because of the king being in check, I'm not so sure, but I'll speculate. If, in the analogy between chess and war, we think of a check to the king as a wounding attack, less severe than the lethal checkmate but damaging nonetheless, then it's reasonable to insist that the hobbled king can't make a swift, immediate escape via castling. But maybe that's just how I think of it.