Introduction
An error is a condition that causes a disruption in the execution of the program. An error can be a result of circumstances, such as incorrect code or insufficient memory resources. Errors in a java program are categorized into two types, compile-time error and run-time error. The compile-time error occurs when the syntax of a programming language is not followed. Run-time errors occurs during the execution of a program. Such an error is known as exception. An exception can lead to the termination of an application.
Throwable class
The throwable class is the base class of execution in Java. It can only throw exception objects that are derived from the Throwable class. Exceptions and errors are derived from the throwable class.
Exception class
The Exception class represents the conditions that a program should handle. The Exception class has various subclasses, such as classNotFoundException, IllegalAccessException, and RuntimeException.
The classNotFound Exception throws when a class is being referred to, but no definition is found.
Error class
The Error class defines the exception related to the java run-time environment. For example, OutOfMemoryErrror is an error that occurs when there is insufficient system memory to execute a program.
Exceptions
- Checked Exception
- Unchecked Exception
Checked Exception
There are the invalid conditions that occur in a java program due to the problems, such as accessing a file that does not exist or referring a class that does not exist. The checked exception are the objects of the Exception class or any of its subclass excluding the RuntimeException and Error class.
Unchecked Exception
The unchecked exception occurs because of programming errors. The compiler does not force a programmer to handle these exceptions. Such errors should be handled by writing bug-free code.
Implementing Exception
When an unexpected error occurs, Java creates an exception object. After creating the exception object, Java sends it to the program by throwing the exception. The exception object contains information about the type of error and the state of the program when the exception occurred.
- Try
- Catch
- Throw
- Throws
- Finally
- Try with resources
Try and Catch
A try block encloses the statement that might raise an exception and defines one or more exception handlers associated with it. If an exception is raised within the try block, the appropriate exception handler that is associated with the try block processes the exception.
Example
- import java.util.Scanner;
- public class Addition {
- public static void main(String args[]) {
- int num1, num2, result;
- String snum1, snum2;
- Scanner sc = new Scanner(System.in);
- try {
- System.out.println("Enter the 1st number");
- snum1 = sc.next();
- System.out.println("Enter the 2nd number");
- snum2 = sc.next();
- num1 = Integer.parseInt(snum1);
- num2 = Integer.parseInt(snum2);
- result = num1 + num2;
- System.out.println("The result is " + result ");
- }
- catch (Exception e) {
- System.out.println("Please input only numeric values");
- }
- }
- }
Throw
By using the throw keyword, it can throw an exception explicitly. The throw keyword terminates the normal flow of control of the Java code and stops the execution of subsequent statements. The throw keyword transfers the control to the nearest catch block that handles the type of exception being thrown.
- public class ThrowDemo {
- void display() {
- throw new RuntimeException();
- }
- public static void main(String args[]) {
- ThrowDemo obj1 = new ThrowDemo();
- try {
- obj1.display();
- } catch (RuntimeException e) {
- System.out.println("Runtime Exeption raised");
- }
- }
- }
Throws
The throw keyword is used by a method to specify the types of exceptions that the method can throw. If a method is capable of raising a checked exception that it cannot handle, then the method must specify that the exception is handled by the calling method.
- public class ThrowsDemo {
- void display() throws Exception {
- throw new Exception();
- }
- public staic void main(String args[]) {
- ThrowsDemo obj1 = new throwsDemo();
- try {
- obj1.display();
- } catch (Exception e) {
- System.out.println("Runtime Exception raised");
- }
- }
- }
Finally
During the execution of a Java program, when an exception is raised, the rest of the statements in the try block are ignored. It is necessary to execute certain statements irrespective of whether an exception is raised. The final block is used to execute these required statements.
Try with Resources
The try-with-resources is similar to the try block. It is essentially used to declare and automatically close the objects, such as the file streams and database connections after the execution of the try blocks finishes. The try with resources block ensures that one or more system resources are released when they are no longer required.