Software Development:- Function Declaration – Exceptions – Java

Background

In the course of software application development, a good engineer will have a read of errors that can be encoutered when the application is ran.

 

Exceptions

Type of Exceptions – Checked Exception Versus Unchecked Exception

Outline

Let us review exceptions based on whether they are “checked” or “unchecked“.

 

Checked Exceptions

Definition

Checked Exceptions are checked at compile time.

If an invoked method can experience an exception, the calling module has two choices.

The choices are:-

  1. Catch the exception within the calling function
  2. Do not catch the exception; thus allowing it to be caught higher up

 

Checked Exceptions can further be divided into:-

  1. Fully Checked
  2. Partially Checked Exception

 

Fully Checked

  1. Checking Depth
    • All its child classes are checked
  2. Examples
    • IOException
    • InterruptedException

 

Partially Checked Exception

  1. Checking Depth
    • Only some of its its child classes are checked
  2. Examples
    • Exception ( Generic )

 

Unchecked Exceptions

Definition

Unchecked exceptions are those exceptions that are not caught when the application’s source code is compiled.

The errors can only be experienced at runtime.

Examples

  1. Arithmetric
    • Divide By zero
    • Arithmetric Overflow Exception

Diagrams

Here are diagram(s) that shows flowcharts on the type of errors that we can encounter.

GeeksforGeeks.Org

Checked vs Unchecked Exceptions in Java

Link

Sample Code

Java

Oracle – Tutorials

Specifying the Exceptions Thrown by a Method

Link

Code
Original

public void writeList() 
{
    PrintWriter out = new PrintWriter
                          (
                              new FileWriter("OutFile.txt")
                          );

    for (int i = 0; i < SIZE; i++) 
    {
        out.println("Value at: " 
                       + i + " = " 
                       + list.get(i)
                   );
    }

    out.close();

}

 

Revised

public void writeList() 
throws IOException, IndexOutOfBoundsException
{
    PrintWriter out = new PrintWriter
                         (
                             new FileWriter("OutFile.txt")
                         );

    for (int i = 0; i < SIZE; i++) 
    {

        out.println("Value at: "
                        + i + " = " 
                        + list.get(i)
                   );

    }
    out.close();
}
Explanation
  1. In Java
    • If exceptions are throwable, but caught within the calling function, they have to be specially marked in the function’s declaration
    • In the example above, the code block
      • throws IOException, IndexOutOfBoundsException
      • is used
    • In terms of the compilation
      • IOException
      • is required
    • During execution
      • list.get(i)
      • will throw an exception if the list’s number of elements is less than SIZE

 

References

  1. GeeksforGeeks.Org
    • Checked vs Unchecked Exceptions in Java
      Link
  2. Oracle
    • The Java Tutorials
      • Exceptions
        • Specifying the Exceptions Thrown by a Method
          Link

One thought on “Software Development:- Function Declaration – Exceptions – Java

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s