Exceptions in Java

Exceptions in Java


 • Exception is an abnormal condition that arises in the code sequence.

• Exceptions occur during compile time or run time.

• “throwable” is the super class in exception hierarchy.

• Compile time errors occurs due to incorrect syntax.

• Run-time errors happen when

– User enters incorrect input

– Resource is not available (ex. file)

– Logic error (bug) that was not fixed



Exception classes


• In Java, exceptions are objects. When you throw an exception, you throw an object. You can't throw just any object as an exception, however -- only those objects whose classes descend from Throwable.

• Throwable serves as the base class for an entire family of classes, declared in java.lang, that your program can instantiate and throw.

• Throwable has two direct subclasses, Exception and Error.

• Exceptions are thrown to signal abnormal conditions that can often be handled by some catcher, though it's possible they may not be caught and therefore could result in a dead thread.

• Errors are usually thrown for more serious problems, such as Out Of Memory Error, that may not be so easy to handle. In general, code you write should throw only exceptions, not errors.

• Errors are usually thrown by the methods of the Java API, or by the Java virtual machine itself.


Post a Comment (0)
Previous Post Next Post