Errors and Exceptions

Data Types and Exceptions in Java

Jim White

Java Developer

Exceptions vs Errors

  • Error is a serious issue or problem
  • Applications cannot handle and recover from errors
    • Errors cause the application to "crash" or stop
    • Example: an out of memory problem is an error in Java
  • Applications can handle and recover from exceptions
    • Try to access elements outside the array size is an exception.
Data Types and Exceptions in Java
Example Error
import java.util.*;

public class CauseOutOfMemory {
    public static void main(String[] args) {
        List<Long> numbers = new ArrayList<Long>();
        long counter = 0;
        while (true) {
            numbers.add(counter);
            counter++;
        }
    }
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.base/java.lang.Long.valueOf(Long.java:1204)
    at foo/exceptions.CauseOutOfMemory.main(CauseOutOfMemory.java:12)
Data Types and Exceptions in Java

Two types of exceptions

Checked Exception Runtime ("unchecked") Exception
Require handling Yes No
General cause Things outside our control Our programming mistakes
Recovery Can usually recover when anticipated Usually unrecoverable
Recovery mechanism try/catch or throws Best to code carefully to avoid them
Example exception File not found Array index out of bounds
Data Types and Exceptions in Java

Checked and Runtime exceptions

  • Exception subclasses not also subclasses of RuntimeException are checked exceptions
    • Must be dealt with (i.e. handled)
    • Ex: file not found (FileNotFoundException)
  • RuntimeException superclass for all unchecked exceptions
    • Do not require handling
    • Ex: accessing an array index that does not exist (IndexOutOfBoundsException)
    • Ex: trying to divide by zero (ArithmeticException)
Data Types and Exceptions in Java

RuntimeException

RuntimeException Subclass Description of when thrown
ArithmeticException Bad arithmetic calculations like dividing by 0
IndexOutOfBoundsException Index used on an array, String, etc. is out of range
NegativeArraySizeException Trying to create an array with a negative size
int y = 5/0; // ArithmeticException causing code
int[] list = new int[-1];  //NegativeArraySizeException causing code
Exception in thread "main" java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.NegativeArraySizeException: -1
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/RuntimeException.html
Data Types and Exceptions in Java

"Checked" Exception

public class LoadClass {
    public static void main(String[] args) {
        Class myClass = Class.forName("com.mysql.Driver");
    }
}
LoadClass.java:3: error: unreported exception ClassNotFoundException; 
must be caught or declared to be thrown
                Class myClass = Class.forName("com.mysql.Driver");
                                             ^
1 error
1 See https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Exception.html
Data Types and Exceptions in Java

Let's practice!

Data Types and Exceptions in Java

Preparing Video For Download...