Often, beginner chess players learn that you can’t castle through check. This wording is of course correct, but sometimes players think of the rule incorrectly.
Two pieces are involved in castling: the King and the Rook. It is true that the King cannot castle through a checking square, but the Rook can pass through that square. Of course, the Rook can never be checked, so the Rook is not really castling through check, but it can pass through the attacked square.
In the first video I show an example from a game that I just played. In the second video, you can see an example from someone else’s game, which was turned into a tactic on Chess.com’s Tactics Trainer.
Video 1: Castling Theme Shown in One of My Recent Games
Video 2: Castling Theme Shown in a Tactics Trainer Puzzle
Just remember that, even though it is possible to castle this way, be very careful–it is often very dangerous to try castling when your King cannot move further to safety, so you need a concrete plan of how you will keep your King safe in this situation.