Chapter:Exceptions - What is it - Exercises

From Juneday education
Jump to: navigation, search

Exercises for what is an Exception

This section is more of a theoretical nature and we haven't learned enough syntax for coding exercises yet. But we have learned a little about the two main families of Exceptions - checked exceptions and runtime exceptions (unchecked). Enough to read about a class in the online documentation and look at a constructor which the documentation says throws an exception (i.e. that it might throw an exception).

So there are just two simple exercises for this section to warm you up (and to encourage you to read the documentation about the Java API classes!).

Q1

Let's look at the API which comes with your Java installation. Look at the documentation for the class Scanner (in the java.util package). Read the description of the class to understand what it is used for. Now, locate the description of the constructor which takes one single argument in the form of a File. What is the exception it is declared that this constructor throws? Is it a checked exception or a runtime exception?

Hint: If the exception class extends Exception and not RuntimeException, it is a checked exception. Follow the link to the exception class to see the inheritance hierarchy of this class.

Expand using link to the right to see a suggested answer/solution.

The constructor in question (the one which takes a single argument of type reference to File) declares that it throws a FileNotFoundException. When we look at the documentation for this class of exceptions, we'll see the following inheritance tree:

java.lang.Object
  java.lang.Throwable
    java.lang.Exception
      java.io.IOException
        java.io.FileNotFoundException

This means that FileNotFoundException is an IOException, which is an Exception! We immediately see that it doesn't extend RuntimeException anywhere in the tree, so it must be a checked exception. Simple, eh? If it doesn't extend at any point RuntimeException, it is a checked exception.

The declaration of this constructor is actually (note the throws keyword):

public Scanner(File source) throws FileNotFoundException

Declaring that a method throws some exception, is almost always used for checked exceptions, so that users of the class can figure this out just by looking at the prototype (declaration) of the constructor or method. And such exceptions, checked exceptions, are enforced by the compiler according to the handle-or-declare rule. Even if we can declare that a method or constructor also throws a runtime exception, the compiler wouldn't care, since they are the exception (no pun intended) to the rule of handle-or-declare.

Q2

Describe with your own words, why you think the constructor from the previous question throws this specific exception type. If it didn't throw an exception, what do you think would happen?

Expand using link to the right to see a suggested answer/solution.

We can't give you your own words, but we'll provide an example of a feasible answer explaining what would happen if the constructor didn't throw an exception when something is wrong.

The Scanner class is used to parse (analyze and interpret) text. The text might be connected to some file, which is the reason for this constructor to exist - we can create a new Scanner and give a file object reference as an argument to this constructor. The Scanner object would then use the file represented by the File object as the source to read and parse text from. Luckily for us, the constructor throws an exception of type FileNotFoundException if the File object represents a File which doesn't exist on our hard drive. This is actually a feature, because it indicates that the parsing (reading and interpreting text) from this file won't work, for the simple reason that the file doesn't exist. Rather than letting us create a Scanner believing that it can read from the file represented by the File object, it throws a checked exception so that we will have to react on this case, rather than trying to scan a file which isn't there.

Links

External links

Where to go next

The next page is: Exceptions_-_Two_main_types

« PreviousBook TOCNext »