An approach to handling errors

Dealing With Errors

     If you're a programmer, you know how difficult it can be to stay motivated when we see errors as we start our journey as a junior developer. From experience I can understand how it can make us feel as if we're not meant to be a programmer. Today I'm going to show you different approaches to handling errors. Most programs have different wordings on the errors being shown but don't let it confuse you. At the end of the day they're just synonyms for the same type of error.

Types of Errors

     As you start out programming, you'll encounter some of the following error: Syntax/NameError, Logic errors, and Runtime errors such as an infinite loop.

RunTime Errors

     Runtime errors are errors that are found while compiling your code. This is the general term for the errors mentioned later. Most errors fall under this category since we won't find errors until we compile. One example of a runtime error is and infinite loop. This will cause you program to crash and break. Infinite loops can be cause in multiple ways especially in ways we don't expect it. One example and more obvious example of an infinite loop is:





     In my counter I'm adding x by 0 which will never increment so it will forever run this loop. The problem with these time of issues is that your terminal may never stop and keep on running forever causing a strain on your cpu. Another example and a less obvious one is the following:


     Here the code is going into an infinite loop as well. The reason for it to go into an infinite loop is because in the if statement we're using the wrong comparator sign. The statement is confusing with setting the value of x to three and actually comparing it to see if it is equal to 3. In this case our mistake is more a syntax/logic error and it may be a hard one to catch but luckily when this happens, always check what's associated with the loop.

Syntax/Name Error

  This is one of the most common types of errors. It happens to all of us especially when we're having to keep track of multiple variables, methods, and functions. A syntax/name error is simply a mismatch in names. In the following example we have a error called NoMethodError which simply states it doesn't recognize the function being called because its misspelled.




     Here you can see that print_name doesn't exist as a method so it won't be able to work. Since programming is very strict on calling the methods name very specifically it will error out if you don't. On the bright side, most programs sort of guide you to figure out the issue and points you to a method/function that's almost similar as to what you were calling it. This next example is in c++. The program had a red line on line 12 hinting at me saying "Hey we don't recognize this function in the file".



Logic Errors

     Logic errors are the hardest ones to spot. A logic error is an error that doesn't pop up as an error. Your program runs but you get the wrong output but syntax wise its all fine. This is where troubleshooting tools come in handy. In ruby we have pry, in python we have a tool called code.interact, javascript we have the tool (which is needed to use with node.js) node inspect <file_name.js>. These all would break your code at the location you want and allow you to play around with your functions/methods/variables without saving it to your actual program. It's very useful when we want to backtrack to see where the wrong output is being given.

  One useful way of using it is by starting at the beginning of your function and use the debugging tools to make sure you're getting the right output. If the output is correct, you can keep going until you finally spot where the logic isn't working the way its supposed to. It could be the math is wrong, wrong use of a variable, etc. Its great that we have these tools to make debugging easier and not have to output every line and make our code messy/unreadable. 

Take Aways

     We shouldn't get stressed out when the program errors out on us when we run the program. Error messages are there to help us out and hints to us where the error could be. Take the time to read the error carefully and first identify which  type it is. Go back to your code and see if the error makes sense to begin with. If you believe everything's fine, you can go to Google/StackOverFlow and see if anyone had a similar error. Again we're not alone when it comes to errors, everyone from all kinds of levels get them. With troubleshooting comes a more understanding of our code and we can stay away from being confused.



Comments

Popular Posts