Stackify is now BMC. Read theBlog

Software Error vs Exception – In Real World Examples

By: mwatson
  |  April 7, 2017

After 15+ years of software development, I still use the words error and exception interchangeably.

But is there a difference between error and exception?

I think it is best to make the distinction with some examples of errors vs exceptions.

Example #1: Dishwasher Errors

Let’s use this example of a dishwasher. Both images depict a problem, or error, that happened with the dishwasher. One was clearly user error of putting in the wrong type of soap. The other was an error that the dishwasher recognized and showed an error code for this.

I think this dishwasher example perfectly sums up the difference between an error and an exception. Both of these problems were errors. The difference here is the designer and engineers designed the product to handle one of the errors. It was an exception that the engineering team knew how to detect and handle. The engineering team did not detect or handle the soap problem.

Exceptions are a type of error that is expected or known to occur.

Example #2: Software Avoiding a Car Crash

Let’s take this example of a Tesla crash. The software within the car was able to detect a stopped vehicle. The software was designed to recognize this as a known problem, or as an exception to normal behavior. The car was not able to completely prevent the crash. But, the developers were able to execute special logic to slow the car down to at least minimum the effect of the problem. Exception handling logic is key to good software. As developers, we have to anticipate the types of errors that may occur and handle them properly.

tesla stopping

Example #3: ATM

When you go to an ATM to withdraw money, you expect to actually get money, right? What if you don’t have any money in your account? That would be a good example of an InvalidBankBalanceException.

If the developers didn’t handle this correctly, they could be handing out money to people who shouldn’t be receiving it.

134979

Or what if the ATM itself didn’t have any money? It would not be good if they deducted $100 from your account for the money you were withdrawing, but it didn’t actually give you any.

20151221_061614-1024x576

How to Anticipate Application Errors

One of the keys to good software is good error and exception handling. It is a good best practice to always be on the defense as your write code. You have to pretend that everything is going to fail.

If you don’t handle possible error scenarios, inevitably, they will happen and your users will have problems. Granted, some of them might be user errors and there may not be anything you can do about them. The dishwasher soap example is a good example of this. Of course, my favorite says is “if it can be null, it will be null”.

Always anticipate possible exceptions and handle them properly.

public static void DoIt(string url)
{
	try
	{
	   WebClient client = new WebClient();
	   string response = client.DownloadString(url);
	}
	catch (WebException ex) when (ex.Response != null)
	{
		var response = ex.Response as HttpWebResponse;
		Console.WriteLine("Error receiving web response. Status: " + response?.StatusCode);
	}
	catch (WebException ex) 
	{
		Console.WriteLine("Error connecting to remote server: " + ex.Message);
	}
	catch (Exception ex)
	{
		Console.WriteLine("Unknown error occurred: " + ex.Message);
	}
}

Be sure to check out our guide to exception handling.

Conclusion

I hope you enjoyed this real world guide to the difference between error and exception. As a developer, part of our challenge is trying to anticipate all the problems that could happen. Sadly, we never can anticipate all of them. This is why all developers need a good error monitoring system.

soccer-save-gif-4

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to stackify@stackify.com