Compare the Difference Between Similar Terms

不同ence Between

首页 / Technology / IT / Systems /不同ence Between Checked Exception and Runtime Exception

不同ence Between Checked Exception and Runtime Exception

July 17, 2011Posted byIndika

检查异常和运行时异常

Exceptions are special type of events, 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. 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). 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, 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. Checked Exceptions are exceptions on which handling is enforced by the compiler. Runtime exceptions are a type of exceptions, which are not checked by the compiler.

What is a Checked Exception?

Checked Exceptions are either objects of the class java.lang.exception or its subclasses (except the java.lang.RuntimeException and its subclasses). Checked exceptions are “checked” at compile time. That means the programmer must either catch or throw these exceptions, or else the compile would complain (causing a compiler error). Because of this reason, many checked exceptions are very well known to programmers. For example, the IOException and its sub classes are checked exceptions, and whenever the programmer is dealing with accessing or modifying a file, compiler checks to make sure that all possible IOExceptions are taken care of by the programmer.

What is a Runtime Exception?

Runtime Exceptions consist of java.lang.RuntimeException and all its sub classes. Theoretically speaking, they serve the same purpose as checked exceptions, and can be thrown or handled like checked exception, but the handling of them is not enforced by the compiler. Therefore, Runtime exceptions belong to the family of unchecked exceptions. NullPointerException, NumberFormatEception, ClassCastException and ArrayIndexOutOfBoundsException are common runtime exceptions in Java.

What is the difference between Checked Exception and Runtime Exception?

Although, both checked exceptions and runtime exceptions are unwanted occurrence during the execution of a program, they have their differences. Checked exception handling is enforced by the compiler, but runtime exceptions are not. Therefore, checked exceptions must to be thrown or handled in order for the code to compile, but there is no such requirement regarding runtime exceptions. Consequently, runtime exceptions belong to unchecked exceptions category along with errors.

Disadvantage with checked exceptions is that the programmer has to handle it even if she does not know how to. So, if the programmer just throws a new exception without wrapping the original, the stack trace belonging to the original exception will be lost. This is where runtime exceptions come in handy. Because all runtime exceptions can be handled in a single place, thus programmers can write less amount of code. On the other hand, since checked exceptions must be caught, there is no surprise for the programmer. She will always know which checked exception could be thrown by a certain method. Contrary to this, various runtime exceptions can be thrown without the knowledge of the programmer.

Related posts:

异常和错误的区别 不同ence Between Interrupt and Exception 不同ence Between System Call and Interrupt 不同ence Between Interrupt and Trap 不同ence Between Backup and Archive

Filed Under:SystemsTagged With:catching,Checked exception,Checked Exceptions,exception handler,exceptions,IOExceptions,Runtime exception,Runtime exceptions,runtime system,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

不同ence Between Coronavirus and Cold Symptoms

不同ence Between Coronavirus and Cold Symptoms

不同ence Between Coronavirus and SARS

不同ence Between Coronavirus and SARS

不同ence Between Coronavirus and Influenza

不同ence Between Coronavirus and Influenza

不同ence Between Coronavirus and Covid 19

不同ence Between Coronavirus and Covid 19

You May Like

不同ence Between Demonstrative Pronoun and Demonstrative Adjective

不同ence Between Demonstrative Pronoun and Demonstrative Adjective

不同ence Between Anger Aggression and Violence

不同ence Between Anger Aggression and Violence

不同ence Between Libertarian and Republican

不同ence Between Libertarian and Republican

不同ence Between Capacitor and Condenser

不同ence Between Base Rate and BPLR Rate

Latest Posts

  • What is the Difference Between Prevention and Cure
  • What is the Difference Between DSM IV and DSM V Autism
  • What is the Difference Between Molecular Formula and Structural Formula
  • What is the Difference Between Acne and Eczema
  • What is the Difference Between Mutation Rate and Substitution Rate
  • What is the Difference Between Vermicompost and Compost
  • 首页
  • Vacancies
  • About
  • Request Article
  • Contact Us

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