Exceptions are an integral part of Java programming, used to handle error conditions and unexpected events that occur while running a program. In Java, exceptions are classified into two categories: checked and unchecked exceptions. In this article, we will discuss the difference between checked and unchecked exceptions in Java.
Checked exceptions are exceptions that are checked at compile time by the Java compiler. If a method throws a checked exception, the compiler requires the method to either handle the exception or declare it in the method signature using the "throws" keyword. Checked exceptions are typically those that are recoverable and can be handled within the program. Examples of checked exceptions include FileNotFoundException, IOException, and SQLException.
Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked by the compiler. They occur at runtime and can be caused by a variety of factors, such as invalid input, incorrect data, or program bugs. Unlike checked exceptions, unchecked exceptions do not need to be declared in the method signature and are not required to be handled by the program. Examples of unchecked exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and NumberFormatException.
Compiler checking: Checked exceptions are checked by the compiler at compile time, while unchecked exceptions are not checked by the compiler.
Method signature: Methods that throw checked exceptions must declare them in the method signature using the "throws" keyword, while methods that throw unchecked exceptions do not need to declare them in the method signature.
Recovery: Checked exceptions are typically recoverable and can be handled within the program, while unchecked exceptions are typically unrecoverable and cannot be handled within the program.
Performance: Unchecked exceptions are faster than checked exceptions as they do not require any checking at compile time.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.