Classifying gauge anomalies through symmetry-protected trivial orders and classifying gravitational anomalies through topological orders