Java Exception Handling and Its Useful Keywords

Java Exception Handling and Its Useful Keywords

Exception handling is a process to handle exceptions in the execution of the program at run time so that the execution should not stop. The advantage of exception handling is to maintain the execution of the program/code because generally, exception stops the execution of the program.

Exception in Java are as below-

1.Checked Exception

2.Unchecked Exception

Checked Exception- Java Classes that inherit Throwable classes except the Runtime Exception and error are called as Checked Exception. The example of these types of exceptions are -

SQL Exception, IOException, Interrupted Exception, Protocol Exception, etc. All these types of Exceptions are checked at compile time.

Below code could throw IOException-

public void createFile(String way, String text) throw IOException {

FileWriter writ = new FileWriter(way, true);

writ.write(text);

writ.close();

}

Unchecked Exception- Java Classes that inherit RunTime Exception are called as Unchecked Exception. Examples of unchecked Exception are ArithmeticExceptions, NullPointerException, ArrayIndexOutOfboundException, etc. These types of errors are not checked at compile-time, they are checked at runtime.

Example of Unchecked Exception-

public static void setAge(int age){

if (age<1 || age>>99)

throw new IllegalArgumentException(“Invalid age”)

}

int newAge = age;

}

Below are some Keywords that are used to handle exceptions In Java-

1.Try- It is a block of code where users write code that could have any exception. This block is always followed either by a catch block or finally block, we cannot use try block alone.

2.Catch- In this block code is written to handle the exception. Catch block is never written alone it is always preceded by the try block, and also catch block could be followed by a finally block.

3.Finally- This block of code is used to execute the crucial code of the program, it is executed when an exception is handled or not handled.

4.Throw- This keyword is used to throw the exception.

5.Throws- This keyword is used to declare the exception, it signifies that this method can throw an exception. We will always write this keyword with the method signature.

Conclusion

A programmer should always handle the exception to maintain the normal execution of the program because an exception can disrupt the normal flow of the execution of the code that’s why we should use exception handling.

At Oodles ERP, we provide full-scale enterprise application development services to efficiently manage diverse business operations. Our custom mobile and services are conducive to increasing business productivity and improving operational efficiency. For more information, contact us at erp@oodlestechnologies.com.

Exception handling is a process to handle exceptions in the execution of the program at run time so that the execution should not stop. Let’s get familiar with some useful keywords for Java exception handling.

We are a leading ERP development company in India offers a wide range of ERP software development services to help you grow your business exponentially.