Retrace will reach End of Life on March 31, 2027. Click here to learn more.

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
indobet toto slot toto toto bobatoto daftar logototo patentoto situs toto slot 4D toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k slot sbobet88 sbobet88 https://nolimithoki.dev/ dinasti33 slot slot jepang https://www.juara288.asia/ slot 4d toto toto situs toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d slot gacor bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot pajaktoto slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs slot link slot situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor situs gacor slot gacor leon188 slot gacor slot gacor slot gacor Slot88 slot gacor Slot88 situs slot slot gacor poker online slot88 Slot88 slot gacor slot terbaru slot gacor situs gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast Jpmania slot resmi slot slot resmi slot bayitoto patentoto link gacor slot gacor kari4d slot gacor AMANAHTOTO slot gacor slot gacor StreamEast AMANAHTOTO pascol4d kari4d AMANAHTOTO kari4d bragatoto dokter88 slot gacor slot game patentoto pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d roma77 judi bola slot depo 10k mpo slot Girang4d KARI4D StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k
indobet toto slot toto toto bobatoto daftar logototo patentoto situs toto slot 4D toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k slot sbobet88 sbobet88 https://nolimithoki.dev/ dinasti33 slot slot jepang https://www.juara288.asia/ slot 4d toto toto situs toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d slot gacor bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot pajaktoto slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs slot link slot situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor situs gacor slot gacor leon188 slot gacor slot gacor slot gacor Slot88 slot gacor Slot88 situs slot slot gacor poker online slot88 Slot88 slot gacor slot terbaru slot gacor situs gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast Jpmania slot resmi slot slot resmi slot bayitoto patentoto link gacor slot gacor kari4d slot gacor AMANAHTOTO slot gacor slot gacor StreamEast AMANAHTOTO pascol4d kari4d AMANAHTOTO kari4d bragatoto dokter88 slot gacor slot game patentoto pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d roma77 judi bola slot depo 10k mpo slot Girang4d KARI4D StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k