Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between an error and an exception in .NET?

Tags:

Could you please explain to me what the difference is between an error and an exception?

like image 467
Amit Avatar asked Mar 12 '10 19:03

Amit


People also ask

What is the difference between an error and exception?

The error indicates trouble that primarily occurs due to the scarcity of system resources. The exceptions are the issues that can appear at runtime and compile time. 2. It is not possible to recover from an error.

What is the difference between error and exception in C#?

what is the difference between error and exception? Error: This is the syntax problem, which leads to complation problem. Exception:- This is the run time error which interrupts the application execution.

What is difference between error and exception Mcq?

What is the difference between error and exception? Explanation: Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.

What is the difference between errors and exceptions in Javascript?

An error is when something wrong or invalid happens in the code. It can cause a memory error, it is something that should never happen and can't be treated. Whereas an exception throws something when certain conditions are met in the code. It may not correspond to a real error.


2 Answers

An exception is a class that takes advantage of language semantics. As others have stated, exceptions interrupt execution up the stack until caught. An exception can be used to convey an error, but more generally is used to convey that something exceptional has occurred.

Errors, on the other hand, can be exceptional or not.

There are several kinds of errors:

  • User error - this should be handled without an exception
  • Syntax error - this shouldn't compile in statically typed languages (in dynamic languages, they're a little harder to discover)
  • Runtime error - this will either result in an exception, or silently fail (usually creating unexpected results)

Really, exceptions should be limited to handling runtime errors, since a user inputting bad data is not "exceptional." To handle user errors, you should take the following approaches:

  • Prevent bad data from being input (front-end validation)
  • Prevent bad data from being persisted (back-end validation)

Exceptions should be used as a "last line of defense" for user error. If you're writing a persistence layer, you can rely on exceptions to ensure that bad data that falls through validation does not get persisted. You should, however, fix any of these by putting a fix in the validation that prevents the error from occurring in the first place.

like image 181
Michael Meadows Avatar answered Oct 08 '22 04:10

Michael Meadows


An exception is an object of a type deriving from the System.Exception class. It is used in a throw statement to transfer control to a catch clause in a try block somewhere further up the call stack.

An error is just some code or message that you're meant to interpret. The problem with error codes is that you can decide to ignore them:

MethodThatReturnsAnError();
SomeCodeThatShouldNotExecuteOnError();

That call will simply ignore the error code if one is returned. However:

MethodThatThrowsAnException();
SomeCodeThatShouldNotExecuteOnError();

This cannot be ignored, and will transfer control up the stack, past "SomeCodeThatShouldNotExecuteOnError();".

like image 38
John Saunders Avatar answered Oct 08 '22 03:10

John Saunders