Core Java - [Exception-01]

♠ Posted by Unknown in at 01:31

Exception: List the Common Java Exceptions.

An exception is a condition that is caused by a run-time error in the program. When the Java interpreter encounter an error such as dividing an integer by zero, it creates an exception object and throws it (i.e., informs us that an error has occurred).

If the exception object is not caught and handled properly, the interpreter will display an error message and will terminate the program. If we want the program to continue with the execution of the remaining code, then we should try to catch the exception object thrown by the error condition and then display an appropriate message for taking corrective actions. This task is known as exception handing.

The purpose of exception handling mechanism is to provide a means to detect and report an “exceptional circumstance” so that appropriate action can be taken. The mechanism suggests incorporation of a separate error handling code that performs the following tasks:
                       
                        Find the problem (Hit the exception)
                        Inform that an error has occurred (Throw the exception)
                        Receive the error information (Catch the exception)
                        Take corrective actions (Handle the exception)

The error handling code basically consists of two segments, one to detect errors and to throw exceptions and the other to catch exceptions and to take appropriate actions.

Common Java Exceptions :

Exception Type
Cause of Exception

ArithmaticException

Caused by math errors such as division by zero

ArrayIndexOutOfBoundsException

Caused by bad array indexes

ArrayStoreException

Caused when a program tries to store the wrong type of data in an array

FileNotFoundException

Caused by an attempt to access a nonexistent file

IOException

Caused by general I/O failures, such as inability to read from a file.

NullPointerException

Caused by referencing a null object.

NumberFormatException

Caused when a conversion between strings and number fails

OutOfMemoryException

Caused when there’s not enough memory to allocate a new object.

SecurityException

Caused when an applet tries to perform an action not allowed by the browser’s security setting.

StackOverflowException

Caused when the system runs out of stack space.

StringIndexOutOfBoundsException

Caused when a program attempts to access a nonexistent character position in a string.

There are two types of Exception in Java.

  1. Checked Exception:-

The Checked Exceptions are those Exceptions which are known at compile time and programmer knows that it must be handled to execute his java code. This kind of exceptions are throws by java methods so, code can be executed smoothly or written into try..catch block to display proper message and run the program on further coding.

The Checked Exceptions are available into Throwable package->Exception class and its sub classes Exception, excepts RuntimeException class.

The Example of Checked Exception is IOException, ClassNotFoundException, SQLException, etc....

  1. UnChecked Exception :-

The Unchecked Exceptions are those Exceptions which are expected at runtime in code segment of java program. The programmer can be guessed his code segment and put it in the try...catch block and if runtime exception occurred by user of that code the catch block can be directed on write error message and program cannot be terminated and go further execution.
The UnChecked Exceptions are available into Throwable Package->Exception Class -> RuntimeException Class and Its Subclasses.

The Example of UhChecked Exceptions are ArrayIndexOutofBoundExcep tion, NumberFormatException, NullPointerException, etc....

0 comments:

Post a Comment