Bugs & errors will always happen and they are a good thing consider them to be Learning Opportunities
When an error occurs, take the following steps:
The process of fixing bugs is also referred to as debugging
Use your developer console always
Google is your friend (when you get stuck)
Use pattern matching
Use good indentation
Break things on purpose (after you’ve gotten it to work) to answer your what if / what would happen if questions
Practice, practice, practice