![]() |
VOOZH | about |
Types of Exceptions in Java Built-in exceptions are the exceptions that are available in Java libraries. These exceptions are suitable to explain certain error situations. Below is the list of important built-in exceptions in Java.
Examples of Built-in Exception:
1. Arithmetic exception : It is thrown when an exceptional condition has occurred in an arithmetic operation.
Can't divide a number by 0
2. ArrayIndexOutOfBounds Exception: It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
Array Index is Out Of Bounds
3. ClassNotFoundException : This Exception is raised when we try to access a class whose definition is not found.
Output:
ClassNotFoundException4. FileNotFoundException : This Exception is raised when a file is not accessible or does not open.
File does not exist
5. IOException : It is thrown when an input-output operation failed or interrupted
Output:
error: unreported exception IOException; must be caught or declared to be thrown6. InterruptedException : It is thrown when a thread is waiting, sleeping, or doing some processing, and it is interrupted.
Output:
error: unreported exception InterruptedException; must be caught or declared to be thrown7. NoSuchMethodException : t is thrown when accessing a method which is not found.
Output:
error: exception NoSuchMethodException is never thrown
in body of corresponding try statement
8. NullPointerException : This exception is raised when referring to the members of a null object. Null represents nothing
Output:
NullPointerException..9. NumberFormatException : This exception is raised when a method could not convert a string into a numeric format.
Output:
Number format exception10. StringIndexOutOfBoundsException : It is thrown by String class methods to indicate that an index is either negative than the size of the string.
Output:
StringIndexOutOfBoundsExceptionSome other important Exceptions
1. ClassCastException
Output:
Exception in thread "main" java.lang.ClassCastException:
java.lang.Object cannot be cast to java.lang.String
2. StackOverflowError
Output:
Exception in thread "main" java.lang.StackOverflowError3. NoClassDefFoundError
Output:
Note: If the corresponding Test.class file is not found
during compilation then we will get Run-time Exception
saying Exception in thread "main" java.lang.NoClassDefFoundError
4. ExceptionInInitializerError
Code 1:
Output:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
Code 2 :
Output:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
Explanation : The above exception occurs whenever while executing static variable assignment and static block if any Exception occurs.
5. IllegalArgumentException
Output:
Exception in thread "main" java.lang.IllegalArgumentExceptionExplanation:The Exception occurs explicitly either by the programmer or by API developer to indicate that a method has been invoked with Illegal Argument.
6. IllegalThreadStateException
Output:
Exception in thread "main" java.lang.IllegalThreadStateExceptionExplanation : The above exception rises explicitly either by programmer or by API developer to indicate that a method has been invoked at wrong time.
7. AssertionError
Output:
Exception in thread "main" java.lang.AssertionErrorExplanation : The above exception rises explicitly by the programmer or by API developer to indicate that assert statement fails.
IllegalArgumentException occurred: Radius cannot be negative
FileNotFoundException occurred: non-existent-file.txt (No such file or directory)