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:-
- Catch the exception within the calling function
- Do not catch the exception; thus allowing it to be caught higher up
Checked Exceptions can further be divided into:-
- Fully Checked
- Partially Checked Exception
Fully Checked
- Checking Depth
- All its child classes are checked
- Examples
- IOException
- InterruptedException
Partially Checked Exception
- Checking Depth
- Only some of its its child classes are checked
- 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
- 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
Sample Code
Java
Oracle – Tutorials
Specifying the Exceptions Thrown by a Method
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
- 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
[…] Software Development:- Function Declaration – Exceptions – Java Link […]