The exceptional sets in the autonomous and periodic KAM theorems consist of sets of near-resonant frequencies ( omega 1, . . ., omega n)= omega for which the method of proof of the theorems breaks down. They are residual and of measure 0. Their Hausdorff dimension is deduced from that of a related set of well approximable linear forms. The Hausdorff dimension of these linear forms is a special case of a more general number theoretic result on systems of linear forms established recently but a self-contained proof is given. The proof relies on the resonant hyperplanes being reasonably well distributed. A fairly general statistical 'second moment' argument is used but some geometrical ideas are introduced to simplify and improve the original proof. When the number of degrees of freedom is large, the Hausdorff dimension is nearly maximal, so that, although of measure 0, the exceptional sets are, roughly speaking, close to sets of positive Lebesgue measure. The implications for the stability of Hamiltonian systems with many degrees of freedom and for the onset of certain kinds of instability are discussed briefly.