Question: Suppose, a country 'X''s civil airline wants to fly to another country 'Z'. There are three routes. However, the shortest route is 1000km which is through country 'Y'.

Can the country 'Y' block its airspace for civil airlines of 'X' without giving any reason?

If NO, can 'X' go to an international court for this? If YES, what would the penalty for the country 'Y' be?

Answer: 
**Yes,** overflight is a privilege, not a right. A denial might cause the victims to retaliate in a similar manner, by restricting overflights *or other things which are not a right.* These are for example no right to immigrate, no right to trade, no right to transfer money through foreign banks.

**But** most sovereign countries are connected by a web of agreements, treaties, and so on. Breaking one *without explanation* would lead to retaliation in other areas. So when the [US closed their airspace](https://en.wikipedia.org/wiki/September_11_attacks#Immediate_response) after 9/11, there was general understanding. When EU countries [closed their airspace](https://en.wikipedia.org/wiki/Evo_Morales_grounding_incident) to the Bolivian president some years later, there was more irritation because the excuse looked flimsy.
Since this is Politics SE and not Law SE, the answer is a trivial "if they have the means (i.e., guns) yes; otherwise, no".

The court question is best asked over at Law.