What is the Difference Between Error and Exception in C#

The main difference between Error and Exception in C# is that an Error occurs due to unavailability of a system resource while an Exception occurs due to an issue in the program.

There is a distinct difference between Error and Exception in C# although people often use these two terms interchangeably. An error is a situation that occurs because of the absence of a required system resource. An exception is a situation that arises due to a problem in the program. Overall, an error is irrecoverable while an exception is recoverable.

Key Areas Covered

1. What is Error in C#
     – Definition, Functionality
2. What is Exception in C#
     – Definition, Functionality
3. What is the Difference Between Error and Exception in C#
     – Comparison of Key Differences

Key Terms

Error, Exception, C#

Difference Between Error and Exception in C# - Comparison Summary

What is Error in C#

An error is a situation that occurs due to unavailability of system resources. Errors are complex failures, and the programmer cannot handle them in his program. Therefore, an error is irrecoverable. It is an unchecked type because the compiler does not know about its occurrence. Therefore, an error occurs at runtime. Out of memory error, stack overflow error, system crash errors are some common errors. As the programmer cannot handle these errors using his C# program, they cause the program to terminate abnormally. 

What is Exception in C#

An exception is an issue that occurs when a program executes. The exception can occur because of a problem in the C# program, but it is possible to recover them. Dividing a number by zero is an example of an exception. The programmer handles exceptions in C# program by using try, catch and finally blocks.  He can place the code that is likely to cause an exception inside the try block. If an exception occurs, the program executes the statements inside the catch block. The finally block will execute whether an exception occurred or not. The programmer can include operations such as file closing inside the finally block. Moreover, it is not mandatory to have a finally block.

There are two types of exceptions as checked and unchecked exceptions. The compiler knows about the checked exceptions. On the other hand, the compiler does not know about unchecked exceptions.

Difference Between Error and Exception in C#

Figure 1: C# program with an exception

Above is a program with an exception. The variable ‘a’ has the value 10 while ‘b’ has the value 0. Dividing a by b gives an exception. Therefore, the catch block executes and prints the exception. In the end, the finally block is executed.

Difference Between Error and Exception in C#

Definition

An error is an indication of an unexpected condition that occurs due to lack of system resources while an exception is an issue in a program that prevents the normal flow of the program.  Thus, this is the main difference between Error and Exception in C#.

Occurrence

Moreover, an error occurs due to the lack of system resources whereas an exception occurs due to an issue in the program.

Recovery

Recovery is another difference between Error and Exception in C#. An error is irrecoverable while an exception is recoverable.

Handling 

Furthermore, there is no way to handle an error using the program. However, it is possible to handle an exception in a program using keywords such as try, catch, finally.

Classification

An error is classified as an unchecked type whereas an exception is classified as checked and unchecked exceptions. Hence, this is another difference between Error and Exception in C#.

Examples

OutOfMemoryError, StackOverFlowError, and, IOError are some examples of errors while ArithmeticException, SQLException, and, NullPointerException are some examples of exceptions. This is another difference between Error and Exception in C#.

Conclusion

The main difference between Error and Exception in C# is that an Error occurs due to unavailability of a system resource while an Exception occurs due to an issue in the program. In brief, an error is a critical condition that is not possible to handle by a C# program while an Exception is an exceptional situation that is possible to handle by a C# program.

Reference:

1. “C# Exception Handling.” Www.tutorialspoint.com, Tutorials Point, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply