![]() |
![]() |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
In java, there are several ready made Exceptions available in the standard java.lang package where we can use in the program.
All the exceptions are derived from Runtime Exception which are automatically available in the program.
Unchecked exceptions are not checked at compile-time rather they are checked at runtime. Thats why they are called as Runtime Exceptions
This type of exceptions are mostly caused by the faults in the code flow.
Sr.no | Exception Name | Description | Example |
---|---|---|---|
1. | ArithmeticException | Occurs due to incorrect Arithmetic operation | Dividing number by 0 |
2. | ArrayIndexOutOfBoundsException | Array goes out of size | Inserting value in array at size + 1 location |
3. | ArrayStoreException | Storing invalid elements into array | Array is of integers and storing string values. |
4. | ClassCastException | Invalid casting of class | Trying to cast an Integer to a String but, String is not an subclass of Integer |
5. | IllegalArgumentException | Illegal argument used to call a method. | Method accepts integer type argument and we are passing String as argument. |
6. | IllegalMonitorStateException | Illegal monitor operation like waiting on an unlocked thread. | While calling wait(),notify() method this type of error occures because they should be called by their owner of object monitor. |
7. | IllegalStateException | Application is in invalid state | Trying to start the thread at wrong time means When thread is already executing and called again. |
8. | IllegalThreadStateException | Requested operation not compatible with current thread state. | If thread is already started in method and again trying to start it. |
9. | IndexOutOfBoundsException | Occures when some type of index is out of bounds | When ArrayList is empty and we are trying to access it. |
10. | NegativeArraySizeException | Array created with initial negative size | Declared array of size -1 and assigning element at Runtime with negative index. |
11. | NullPointerException | Invalid use of a null reference | When an object have assigned as null reference, and we are trying use that object having null reference. |
12. | NumberFormatException | Invalid conversion of non Numberic values into Numberic format. | Accepting 'hello' as string from commandline argument and casting into Integer type. |
13. | SecurityException | Attempting to disturb the security | Trying to write package name which is already defined in java. Ex- creating package as package java.util |
14. | StringIndexOutOfBounds | Trying to retrive the value from outside the size. | When the string have length of 20 and trying to print the character at index 45. |
15. | UnSupportedOperationException | Unsupported operation was encountered. | When trying to retrieve an unmodifiable view of the ArrayList and also to insert a new element in it, which resulted to this type of exception. |
Checked exceptions are always checked at compile time.
They must be fixed before executing the program. We can do this by using the throws keyword for handling exception of methods.
Sr.no | Exception Name | Description | Example |
---|---|---|---|
1. | ClassNotFoundException | Class not found while compiling the program | While trying to load a class using forName() method which is not found in program. |
2. | CloneNotSupportedException | Attempt to clone an object that doesn't implement the Clonable interface. | While Trying to clone an object of class which does not implemented with clonable interface. |
3. | IllegalAccessException | Preventing a class from access. | When an application tries to create an instance and tries to call a emthod which is not accessible to application. |
4. | InstantiationException | Attempt to create an object of an abstract class or interface. | When the object of abstract class or interface is created in the subclass then this type error occures. |
5. | InterruptedException | One thread has been interrupted by another thread | It is thrown when a thread is waiting or sleeping and another thread interrupts it using the interrupt method in class thread. |
6. | NoSuchMethodException | A requested method does not exist. | Occurs when arguments of function are changed,from object A to to String A. |