Compare the Difference Between Similar Terms

Difference Between

首页 / Technology / IT / Programming /Difference Between Exception and Error

Difference Between Exception and Error

June 23, 2011Posted byIndika

异常和错误

Unexpected behavior is bound to occur when a program is running. This could be due to exceptions or errors. Exceptions are events, which can disturb the normal program flow. Errors are conditions that can be considered irrecoverable. Exceptions are mostly related to the application itself, while errors are related to the system on which the program is running.

What is an Exception?

Exception is an event, which can disturb the normal program flow. The name exception comes from “exceptional event”. Throwing an exception is the process of creating an exception object and handing it off to the runtime system. Exception object is created by the method in which the exception occurred. Exception object contains useful information such as the type and the description of the exception. When the runtime system receives the exception object, it will try to find somebody to handle it within the call stack by traversing it in the reverse order (in which the methods were called). Call stack is the ordered list of methods, which were called prior to the method in which the exception occurred. The runtime system is successful if it finds a method with an exception handler. Exception handler is a block of code that can officially handle the said exception. If the runtime system finds an appropriate handler (i.e. type of exception matches the type that can be handled), it will pass the exception object to the handler. This is called catching the exception. However, if the exception cannot be handled, the program will terminate. In Java, exceptions inherit from ‘Throwable class.’ NullPointerException and ArrayIndexOutOfBoundsException are two common exceptions in Java.

What is an Error?

An error is a condition that can be considered irrecoverable such as the program requiring an amount of memory larger than what is available. These errors cannot be handled at runtime. If an error occurs, the program will terminate. In Java, errors inherit from Throwable class. Errors usually stand for serious problems that the programmer (or the application) should not try to catch. Errors are simply abnormal conditions, which are never expected to occur under normal circumstances, and therefore never foreseen. For example, OutOfMemoryError, StackOverflowError and ThreadDead are such errors. Methods should never have handlers for errors.

What is the difference between Exception and Error?

Both errors and exceptions are unwanted occurrence during the execution of a program. However, they have key differences. Exceptions can be foreseen by the programmer, while an error is difficult to foresee. Exceptions can be checked or unchecked. But errors are always unchecked. Exceptions typically indicate an error caused by the programmer. However, errors occur due to a system error or an inappropriate usage of a resource. Therefore, exceptions should be handled at the application level, while errors should be taken care of at the system level (only if possible). After handling an exception, you are guaranteed to return to normal program flow. But even if an error is caught, the programmer may not know how to handle it in the first place. Unlike traditional error handling, exceptions allow separating error-handling code from regular code.

Related posts:

Difference Between Graph and Tree Difference Between Enumeration and Iterator Difference Between Definite Loop and Indefinite Loop Difference Between Abstract Class and Concrete Class Difference Between Structured Programming and Object Oriented Programming

Filed Under:Programming标记:ArrayIndexOutOfBoundsException,call stack,Error,errors,events,异常,异常handler,异常object,异常s,Java,NullPointerException,OutOfMemoryError,program execution,runtime system,StackOverflowError,ThreadDead,Throwable class

About the Author:Indika

Indika, BSc.Eng, MSECE Computer Engineering, PhD. Computer Science, is an Assistant Professor and has research interests in the areas of Bioinformatics, Computational Biology, and Biomedical Natural Language Processing.

Leave a ReplyCancel reply

Your email address will not be published.Required fields are marked*

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

Difference Between Military and Shoulder Press

Difference Between Roughage and Concentrate

Difference Between Roughage and Concentrate

Difference Between Corrosion and Rusting

Difference Between Sacred and Holy

Difference Between Aldol Condensation and Claisen Condensation

Difference Between Aldol Condensation and Claisen Condensation

Latest Posts

  • What is the Difference Between Aquagenic Urticaria and Aquagenic Pruritus
  • What is the Difference Between Astringent and Toner
  • Eso的区别是什么phagitis and Barrett’s Esophagus
  • What is the Difference Between Alcohol Ink and Resin Dye
  • What is the Difference Between Hyperparathyroidism and Hyperthyroidism
  • What is the Difference Between Pearlescent and Iridescent
  • 首页
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018Difference Between. All rights reserved.Terms of Useand Privacy Policy:Legal.