Exception types

"I wanted to talk about one more thing today. In Java, all exceptions are divided into two types: checked and unchecked (those that must be caught and those you don't have to catch). By default, all exceptions need to be caught."

"Can you intentionally throw exceptions in your code?"

"You can throw any exception in your own code. You can even write your own exceptions. But we'll talk about that later. Right now, let's concentrate on exceptions thrown by the Java Machine."

"OK."

"If ClassNotFoundException or FileNotFoundException are thrown (occur) in a method, the developer must indicate them in the method declaration. These are checked exceptions. This is how it usually looks:"

Examples of checked exceptions
public static void method1() throws ClassNotFoundException, FileNotFoundException

public static void main() throws IOException
public static void main() // Doesn't throw any exceptions

"So we just write 'throws' followed by a comma-delimited list of exceptions, right?"

"Yes. But there's more. For the program to compile, the method that calls method1 in the example below must do one of two things: either catch these exceptions or rethrow them (to the caller), indicating the rethrown exceptions in its declaration."

"Again. If your main method needs to call a method whose declaration contains 'throws FileNotFoundException, …', then you need to do one of two things:

1) Catch FileNotFoundException, …

You must wrap the code calling the unsafe method in a try-catch block.

2) Don't catch FileNotFoundException, …

You must add these exceptions to the throws list of your main method."

"Could you give me an example?"

"Take a look at this:"

Examples of checked exceptions
public static void main(String[] args)
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"The code in this example won't compile, because the main method calls method1(), which throws exceptions that have to be caught."

"To make it compile, we need to add exception handling to the main method. You can do this in one of two ways:"

Option 1: We simply rethrow the exception (to the caller):
public static void main(String[] args)  throws FileNotFoundException, ClassNotFoundException 
{
    method1();
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"And here we catch it with a try-catch:"

Option 2: Catch the exception:
public static void main(String[] args)
{
    try
    {
        method1();
    }
    catch(Exception e)
    {
    }
}

public static void method1() throws FileNotFoundException, ClassNotFoundException
{
    //Throws FileNotFoundException if the file doesn't exist
    FileInputStream fis = new FileInputStream("C2:\badFileName.txt");
}

"It's starting to get clearer."

"Look at the following example. It will help you understand the rest."

Instead of handling exceptions, we rethrow them to methods farther down the call stack who know how to handle them:
public static void method2() throws FileNotFoundException, ClassNotFoundException
{
    method1();
}
Handle one exception and throw another:
public static void method3() throws ClassNotFoundException
{
   try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
}
Catch both exceptions, throw none:
public static void method4()
{
    try
    {
        method1();
    }
    catch (FileNotFoundException e)
    {
        System.out.println("FileNotFoundException has been caught.");
    }
    catch (ClassNotFoundException e)
    {
        System.out.println("ClassNotFoundException has been caught.");
    }
}

"There is one more type of exception, the RuntimeException and classes that inherit it. You don't need to catch them. These are unchecked exceptions. They are considered difficult to predict. You can deal with them in the same way, but you don't need to indicate them in a throws clause."