Exceptions in Java
Exception Handling in Java is one of the effective means to handle the runtime errors so that the regular flow of the application can be preserved. Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions.
Major reasons why an exception Occurs
- Invalid user input
- Device failure
- Loss of network connection
- Physical limitations (out of disk memory)
- Code errors
Blocks & Keywords used for exception handling
1. try: The try block contains a set of statements where an exception can occur.
2. catch: The catch block is used to handle the uncertain condition of a try block. A try block is always followed by a catch block, which handles the exception that occurs in the associated try block.
3. throw: The throw keyword is used to transfer control from the try block to the catch block.
4. throws: The throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.
5. finally: It is executed after the catch block. We use it to put some common code (to be executed irrespective of whether an exception has occurred or not ) when there are multiple catch blocks.
Exception types
Built-in Exceptions:Built-in exceptions are the exceptions that are available in Java libraries.
ArithmeticException: It is thrown when an exceptional condition has occurred in an arithmetic operation.
ArrayIndexOutOfBoundsException: 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.
ClassNotFoundException: This Exception is raised when we try to access a class whose definition is not found
FileNotFoundException: This Exception is raised when a file is not accessible or does not open.
IOException: It is thrown when an input-output operation failed or interrupted
Checked Exceptions
These are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword.
Unchecked Exceptions
These are the exceptions that are not checked at compile time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions. In Java, exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
How to Create User-Defined Exceptions in Java?
To create a custom exception in java, we have to create a class by extending it with an Exception class from the java.lang package.
It's always a good practice to add comments and follow naming conventions to easily identify and recognize the benefit of our exception class.
user-defined exception in java
public class SimpleCustomException extends Exception{
}
Post a Comment
0 Comments