Built-in exceptions are the exceptions which are available in Java libraries. Java defines several exception classes in the standard java.lang
package. The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available.
The following is the Example of Built in Exceptions
class
ArithmeticExceptionDemo {
public
static
void
main(String args[])
{
try
{
int
a = 1
0
, b =
0
;
int
c = a / b;
// cannot divide by zero
System.out.println(
"Result = "
+ c);
}
catch
(ArithmeticException e) {
System.out.println(
"Can't divide a number by 0"
);
}
}
}
class
ArrayIndexOutOfBoundDemo {
public
static
void
main(String args[])
{
try
{
int
a[] =
new
int
[3
];
a[
7
] =
9
;
// accessing 8th element in an array of
// size 3
}
catch
(ArrayIndexOutOfBoundsException e) {
System.out.println(
"Array Index is Out Of Bounds"
);
}
}
}
Output
Array Index is Out Of Bounds
Java defines several other types of exceptions that relate to its various class libraries. Following is the list of Java Unchecked RuntimeException
Numbering | exception class | describe |
---|---|---|
1 | ArithmeticException |
Arithmetic errors, e.g. dividing by zero. |
2 | ArrayIndexOutOfBoundsException |
Array index is out of range (out of bounds). |
3 | ArrayStoreException |
An array element of an incompatible type was allocated. |
4 | ClassCastException |
Invalid type conversion. |
5 | IllegalArgumentException |
A method was called with illegal parameters. |
6 | IllegalMonitorStateException |
Illegal watch operations, such as waiting for an unlocked thread. |
7 | IllegalStateException |
The environment or application is in an incorrect state. |
8 | IllegalThreadStateException |
The requested operation is not compatible with the current thread state. |
9 | IndexOutOfBoundsException |
An index of some type is out of range (out of bounds). |
10 | NegativeArraySizeException |
Create arrays with negative sizes. |
11 | NullPointerException |
Invalid use of a null reference. |
12 | NumberFormatException |
Invalid string converted to number format. |
13 | SecurityException |
Attempt to violate security regulations. |
14 | StringIndexOutOfBounds |
An attempt was made to index outside the bounds of the string. |
15 | UnsupportedOperationException |
An unsupported operation was encountered. |
Following is the list of Java checked exceptions defined in java.lang
Numbering | exception class | describe |
---|---|---|
1 | ClassNotFoundException |
class not found exception |
2 | CloneNotSupportedException |
Attempt to clone an object that does not implement the Cloneable interface. |
3 | IllegalAccessException |
Access Denied class exception. |
4 | InstantiationException |
Attempting to create an object of an abstract class or interface exception. |
5 | InterruptedException |
A thread is interrupted by another thread. |
6 | NoSuchFieldException |
The requested field does not exist. |
7 | NoSuchMethodException |
The requested method does not exist. |