The irrationality exponent of an irrational number $\xi$, which measures the approximation rate of $\xi$ by rationals, is in general extremely difficult to compute explicitly, unless we know the continued fraction expansion of $\xi$. Results obtained so far are rather fragmentary and often treated case by case. In this work, we shall unify all the known results on the subject by showing that the irrationality exponents of large classes of automatic numbers and Mahler numbers (which are transcendental) are exactly equal to $2$. Our classes contain the Thue--Morse--Mahler numbers, the sum of the reciprocals of the Fermat numbers, the regular paperfolding numbers, which have been previously considered respectively by Bugeaud, Coons, and Guo, Wu and Wen, but also new classes such as the Stern numbers and so on. Among other ingredients, our proofs use results on Hankel determinants obtained recently by Han.