answersLogoWhite

0

Difference between checked and unchecked exception?

Updated: 9/22/2023
User Avatar

Wiki User

13y ago

Best Answer

Checked exceptions are exceptions which need to be handled explicitly. These are the ones which require a try-catchblock or a throws keyword.

Unchecked exceptions are exceptions which have no obligation to be handled. A NullPointerException is one common example.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Runtime exceptions are referred to as unchecked exceptions. All other exceptions are checked exceptions, and they don't derive from java.lang.RuntimeException. A checked exception must be caught somewhere in your code. If you invoke a method that throws a checked exception but you don't catch the checked exception somewhere, your code will not compile. That's why they're called checked exceptions; the compiler checks to make sure that they're handled or declared

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between checked and unchecked exception?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the types of exception in java?

Exceptions are of two types: checked exceptions and unchecked exceptions.


What is unchecked exception?

A checked exception is an exception which the Java source code must deal with, either by catching it or declaring it to be thrown. Unchecked exceptions are all exceptions which do not follow this rule. When an unchecked exception is thrown, it is usually caused by a misuse of code - passing a null or otherwise incorrect argument. This includes classes like NullPointerException and IllegalArgumentException. Checked exceptions are generally caused by faults outside of the code itself - missing resources, networking errors, and problems with threads come to mind. These could include subclasses of FileNotFoundException, UnknownHostException, etc. The Java documentation (link below) gives some loose guidelines to follow when trying to decide which type of exception to use: "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." From a purely code-oriented point of view, a checked exception is a subclass of Exception, while an unchecked exception is a subclass of RuntimeException.


Checked and unchecked exceptions?

Unchecked exceptions : * represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time." * are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException * a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so) Checked exceptions : * represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) * are subclasses of Exception * a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow) It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).


If a method calls another method and that method throw an exception then caller method must be throw that exception or not?

If method A calls method B and method B throws an exception, then method A must handle that exception. It does not have to throw the exception if it is in a try-catch block, but it must do something to deal with it.Note that this only applies to checked exceptions. If method B throws an unchecked exception, then A is allowed to ignore it.


Is error and exception same?

Error: Any departure from the expected behavior of the system or program, which stops the working of the system is an error. Exception:Any error or problem which one can handle and continue to work normally. Note that in Java a compile time error is normally called an "error," while a runtime error is called an "exception." Errors don't have subclasses while exception has two subclasses, they are compile time exception or checked exception (ClassNotFound Exception, IOException, SQLException etc.) and runtime or unchecked exception(ArrayIndexOutOfBounds Exception, NumberFormat Exception).


What is the difference between a patient and a guest?

The difference is patients stay too get checked out guest visit them.


What is checked exception?

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.


What is the difference between colt police positive 38spl or 38ctg?

Nothing. Have it checked by a gunsmith to be sure.


How can you tell the difference between a real and counterfeit 1909 VDB Lincoln Penny?

Have it checked by a coin dealer.


What is the difference between transporting pets as cargo and transporting pets as checked baggage?

Cargo goes with you (like a purse) and checked baggage goes in the luggage department which is probably not where you are seated.


What is the difference between catch exception e catch error err and catch throwable t?

In Java it is related to the class hierarchy of exceptions. Throwable is the root object of the heirarchy, and both Exception and RuntimeError subclass it. Methods include a "throws" clause in their signature to indicate errors of type "Exception" that can be thrown in the body of the method and returned to the caller. Errors of type RuntimeError do not need to be reported in the throws clause.Exceptions are also called "checked" errors, and RuntimeErrors are called "unchecked." This simply means that methods that throw checked errors must declare them in the signature.* Exception: this is a "checked" error. Usually, this is some sort of data error that can be handled by the method. An example is FileNotFoundException - some code didn't an expected file, but the program may be perfectly capable of going on.* Error: A more serious problem that a program probably can't deal with. An example is OutOfMemoryError - when there is no more memory, there isn't much you can do about it and not much point in continuing.* Throwable: Superclass of both Error and Exception.So the main difference is that* 'catch Exception' will catch just exceptions (the problems you might deal with)* 'catch RuntimeError' will catch just errors (the problems you probably can't deal with)* 'catch Throwable' will catch all errors.Generally, you want to catch Exception or Throwable, but not RuntimeError. You will only want to catch Throwable if there is a requirement that the method never throw an error back to the caller.See related link to the Java API, and browse the documentation and subclasses of Throwable.


Can any one please give some example of caught-exceptions and uncaught-exceptions in java?

// Open a reader from standard in... final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { // BufferedReader.readLine can throw an IOException, which is a checked exception. // So we need to catch it. String str = in.readLine(); // Integer.valueOf throws a NumberFormatException, which is an unchecked exception. // Note that we don't need to catch this. int num = Integer.parseInt(str); } catch (final IOException ex) { }