If your king is in check, then castling is not a legal move.
If you I asking whether it is legal to
castle out of checkmate if it is the only option then no the same rules apply as with check although I cant really think of many instances where
you would be able to
castle to prevent
checkmate...
read more >>