Here's why it's an idiom:
* Literal meaning: It's literally impossible for a judge to throw a physical book at a defendant.
* Figurative meaning: The "book" represents the law and the full extent of its punishment.
Example:
"The judge threw the book at the criminal, sentencing him to 25 years in prison for his violent crimes."
This implies that the judge imposed the harshest possible penalty allowed by law for the crimes committed.