A desktop Mac that shuts down without any warning is often overheating. Fan failure can cause this.
A portable Mac with certain obscure battery problems can also shut down suddenly, but if it was on a properly-functioning power adapter at the time, this generally can be ruled out.
You should try running Apple Hardware Test, and/or make an appointment to visit the genius Bar. There is no charge for consultations at the Genius Bar. If you need repairs, that work may be extra, depending on your warranty status.