In order to learn and progress, you must properly frame your relationship with failure. In non-academic facets of our lives, we understand that failure, analysis, and incremental improvement are the stones that pave the path to mastery.
Want to master a sport? An instrument? Want to accomplish anything truly difficult and worthwhile? Then you are going to fail; over, and over, and over again. The same is true in academics.
Failure is not your judge; it is your surest and truest teacher. If your goal is mastery, then your aim is simple: to fail better than you failed before.