Answer: An exception is a problem that arises during the execution of a
program. An exception can occur for many different reasons, including the
following:
·
A user has entered invalid data.
·
A file that needs to be opened cannot be found.
·
A network connection has been lost in the middle of communications,
or the JVM has run out of memory.
User Defined
Exception:We can create our own exceptions in Java. Keep the following points
in mind when writing your own exception classes:
·
All exceptions must be a child of Throwable.
·
If you want to write a checked exception that is automatically
enforced by the Handle or Declare Rule, you need to extend the Exception class.
·
If you want to write a runtime exception, you need to extend the
RuntimeException class.
We can define
our own Exception class as below:class MyException extends Exception{
}We just need to extend the Exception class to create our own Exception class. These are considered to be checked exceptions. The following InsufficientFundsException class is a user-defined exception that extends the Exception class, making it a checked exception. An exception class is like any other class, containing useful fields and methods
System defined exception: These are generating from system error
during compile time but do not affect its execution.
These are Raised
implicitely raised by system because of illegal execution of
program,When cannot continue program execution any more.
These type of Exception are Created by Java System automatically.
System defined Exception are extended from Error class and RuntimeException class.
These type of Exception are Created by Java System automatically.
System defined Exception are extended from Error class and RuntimeException class.
Example of System defined Exception are
a)IndexOutOfBoundsException :
– When beyond the bound of index in the object which use index, such as array, string, and vector
b). ArrayStoreException :
– When assign object of incorrect type to element of
array
c). NegativeArraySizeException :
– When using a negative size of array
d). NullPointerException:
– When refer to object as a null pointer
– When beyond the bound of index in the object which use index, such as array, string, and vector
b). ArrayStoreException :
– When assign object of incorrect type to element of
array
c). NegativeArraySizeException :
– When using a negative size of array
d). NullPointerException:
– When refer to object as a null pointer
0 comments :
Post a Comment