A player can't castle if he is in check or if he would move across a square in which he would be in check.
Why?
Simple answer: It's the rules. Plain and simple.
Philosophical answer: That's just how the rules have worked out. If the chess gurus decide to change that, a player would then be able to castle when in check.
why can't a player castle when his king is on check