Exception types

Available
Exception types - 1

"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.");
    }
}
10
Task
Java Syntax,  level 9,  lesson 4
unavailable
Code entry
Your attention, please! Now recruiting code entry personnel for CodeGym. So turn up your focus, let your fingers relax, read the code, and then... type it into the appropriate box. Code entry is far from a useless exercise, though it might seem so at first glance: it allows a beginner to get used to and remember syntax (modern IDEs seldom make this possible).

"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."

Comments(5)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
VahanLevel 20 , Tbilisi
31 July, 08:59
Can we catch exceptions in the method itself instead of method that calls it?
Jen PLevel 26
21 July, 04:35
What is the "runtime" errors exactly ? Can we understand that as any exceptions that pass the compilation, but leads to program terminates?
// Java PoserLevel 18 , Cincinnati
26 March, 02:06
I wish there was an explanation regarding the web map at the top of the page
David J. MalanLevel 19 , Bishkek
3 April, 00:48
Links from the Professor – 9. It will be explained in lecture number 9.
HashiramaLevel 26 , Port-Harcourt
26 January, 10:56
Cool!!!