ehat is the differencde between error,checked exception and unchecked exception?when occurs at what time?compile time or runtime?which of them can be handled and what not handled?example?

Recommended Answers

All 3 Replies

A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method·
Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the
exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method· Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.

Error: Assuming you refer to error codes, these would be errors given to caller of a function via return types.
Checked Exception: Exceptions that are checked by compiler at compile time are the checked exceptions. Means if you are not handling a checked exception of a method you call, compiler would give an error.
These are derived from Exception class.
Unchecked Exception: Exceptions that are NOT checked by compiler at compile time. THese are derived from RuntimeException

If you are trying to decide whether you should derive your own exception class from RuntimeException or Exception, then the rule of thumb would be to see if this is:
- If you are throwing the exception because your function is unable to do it's own work throw a checked exception.
- If you are throwing the exception because of some external factors which don't anything to do with the functionality of your code throw an unchecked exception. (E.g. IllegalArgumentException).

Error (since 1.5) is another "excpetion" class. Here a quote from the API

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.