Also, to correct the assumption that one must be aroused in order to have an erection, it is not always so. During puberty, a surging of hormones is the most likely cause of the erections. Your mind could be on math and the next thing you know is that you feel your snake pocking against you pants. It is generally hard to avoid these, especially for those that are not used to them.
A good way to help prevent these is to masturbate regularly. It doesn't prevent them indefinitely, but it does lessen the frequency of said erections.