Exceptions in Java

Walden Systems Geeks Corner Exceptions in Java tutorial programming Rutherford NJ New Jersey NYC New York North Bergen County
Java Powers the Biggest Technology. Mobile. Web apps. The Internet of Things. Big data. Machine learning. Cloud platform and cloud infrastructure. The next big things are here now, and Java is at the center of them all. Whether you’re developing the robust enterprise back end, building the agile front end, or thriving in a DevOps role, Java skills can up your game. With more than 10 million programmers and 13 billion Java-enabled devices worldwide, Java is the language that connects applications to data, data to people, and people to their digital lifestyle.

An exception is a problem that occur during the execution of a program. When an Exception occurs, the normal flow of the program is disrupted and the program terminates abnormally, which is not recommended, therefore, these exceptions should be handled. An exception can occur for many different reasons. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications or the JVM has run out of memory. A user has entered an invalid data. Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed. As a result, we have three categories of Exceptions. In this tutorial, we will explain these three categories.

Checked exceptions

A checked exception is an exception that is checked by the compiler at compilation-time, these are also called as compile time exceptions. These exceptions cannot be ignored, the programmer should take care of these exceptions. An example of a checked exception is if a file specified in the constructor doesn't exist. When that happens, a FileNotFoundException occurs and the compiler will complain that the programmer should handle the exception.

import java.io.File;
import java.io.FileReader;

public class ReadFile
{

   public static void main(String args[]) 
   {		
      File file = new File("C://foo.txt");
      FileReader fr = new FileReader(file); 
   }
}


When we try to compile this program, the compiler will give the following exception if the file doesn't exist.

ReadFile.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fr = new FileReader(file);


Unchecked exceptions

An unchecked exception is an exception that happens during the execution of the program. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. An example of an unchecked exception is when we try to access or add an array element larger than the size of the array.

public class MyArray 
{
   
   public static void main(String args[]) 
   {
      int num[] = {1, 2, 3, 4};
      System.out.println(num[5]);
   }
}


When we compile and execute the code, we will get the following error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at Exceptions.MyArray.main(Unchecked_Demo.java:8)

Errors

These are not exceptions but are problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because we can't do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

Catching exceptions

We test for exceptions using a combination of the try and catch keywords. A try / catch block is placed around the code that might have an exception. Code within a try/catch block is referred to as protected code. The code which may have exceptions is placed in the try block. When an exception occurs, that exception is handled by catch block associated with it. Every try block should be immediately followed either by a catch block or finally block. A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in protected code, the catch block that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.

public class MyArray 
{
   
   public static void main(String args[]) 
   {
      int num[] = {1, 2, 3, 4};
      try
      {
          System.out.println(num[5]);
      }
      catch ( ArrayIndexOutOfBoundsException e )
      {
          system.out.printline( "Error : " + e ) ;
      }
      . . .
   }
}


In this case, the code will output an error and continue with the program.