Chapter:Exceptions - Two main types - Exercises

From Juneday education
Jump to: navigation, search

Exercises - Two main types of exceptions

1 - Give examples of runtime exceptions

Go to the API documentation page for the class RuntimeException. Give five examples of subclasses to RuntimeException. This can be found under RuntimeException's documentation page section called "Direct Known Subclasses". Did you recognize any from the lecture slides?

Expand using link to the right to see the full content.

For instance:

  • ArithmeticException
  • ClassCastException
  • NullPointerException
  • IllegalArgumentException
  • NegativeArraySizeException

The first three were mentioned in the lecture.

Please follow the links in the API to the classes you chose, and read the description about the various exceptions.

2 - Find four subclasses to IOException

Go to the API and find four (immediate) subclasses to IOException, using the same technique as above. Write them down and write a short description of each one of the four. Are all subtypes to IOException checked or unchecked?

Expand using link to the right to see the full content.

For instance the following four classes are subclasses to IOException, and since IOException is a checked exceptions, they are too:

  • FileNotFoundException - Signals that an attempt to open the file denoted by a specified pathname has failed
  • FileSystemException - Thrown when a file system operation fails on one or two files. This class is the general class for file system exceptions.
  • MalformedURLException - Thrown to indicate that a malformed URL has occurred. Either no legal protocol could be found in a specification string or the string could not be parsed. (a URL is an address with a protocol like http://virt08.itu.chalmers.se where http is the protocol)
  • UnknownHostException - Thrown to indicate that the IP address of a host could not be determined. (A host is a node, like a computer for instance, on the network, and the IP address of a host is the internet protocol address. The IP address for virt08.itu.chalmers.se is, for instance 129.16.69.98 at the time this text was written)

3 - What is the full package name for some exceptions?

What are the names of the packages for the following exception classes?

  • NullPointerException
  • FileNotFoundException
  • MalformedURLException

What do the package names tell you about the respective classes?

Expand using link to the right to see the full content.

  • NullPointerException - java.lang - this is probably quite common, since classes in java.lang doesn't require import statements
  • FileNotFoundException - java.io - this has probably to do with input/output (IO)
  • MalformedURLException - java.net - this has probably to do with networking

4 - Runtime exceptions can be documented

Go to the class File and look at the constructor which takes a single String reference as argument. What does the documentation say will happen if the value of the parameter pathname is null?

Does the constructor have a throws declaration, that is, does it say that it throws any exception, like the constructor of the Scanner did?

Expand using link to the right to see the full content.

The documentation says in the detailed desctiption:

Throws: NullPointerException - If the pathname argument is null

Note that this is not part of the method declaration! You need to read the detailed description in order to get this information!

The full declaration of the constructor in java.io.File which takes a single String argument is:

public File(String pathname)

As you can see, there is no throws after the argument parentheses, like there was for the Scanner constructor. NullPointer exception is a RuntimeException, and these are usually not part of the declarations, because there is no rules about having to handle-or-declare runtime exceptions enforced by the compiler. Even if the declaration did say throws RuntimeException (or throws NullPointerException), the compiler wouldn't care, since it knows that there is no obligation for calling code to care about such cases.

If you are really curious, here's the full source code for the File constructor in question (from jdk version 7):

    public File(String pathname) {
        if (pathname == null) {
            throw new NullPointerException();
        }
        this.path = fs.normalize(pathname);
        this.prefixLength = fs.prefixLength(this.path);
    }

As you see, a simple if-statement is used to see if the parameter is null. And here you actually can see the code for throwing a NullPointerException (without any error message string). More on syntax in coming sections below!

5 - What is the call stack?

The call stack is a data structure which keeps track of the order methods are calling other methods. In a stacktrace from a crash like the one below, what methods were called in what order? Hint: main is always called first, by the JVM no less!

Exception in thread "main" java.lang.RuntimeException: Important file not found, omg
	at Critical.loadSuperImportantFile(Critical.java:24)
	at Critical.initApplication(Critical.java:13)
	at Critical.main(Critical.java:10)

Expand using link to the right to see the full content.

In this stacktrace, we can see that main was called first (since it is the entry point of any Java application) and main called, on line 10, initApplication. Next, initApplication, on line 13, called loadSuperImportantFile, Which caused a RuntimeException on line 24. The rest of the stacktrace is omitted, since we wanted to focus on code we wrote ourselves.

The call-chain was then: main -> initApplication -> loadSuperImportantFile -> exception!

Exception in thread "main" java.lang.RuntimeException: Important file not found, omg
	at Critical.loadSuperImportantFile(Critical.java:24)
	at Critical.initApplication(Critical.java:13)
	at Critical.main(Critical.java:10)

Links

Where to go next

The next page is: Exceptions_-_Code_examples

« PreviousBook TOCNext »