LAB 2- Handling Exceptions

Goal

Given Spider.java, handle the exceptions with try-catch-finally syntax. If your code runs into a problem that is not adequately described by any of the standard exception classes,(like the object passed to loadModel() method is not a SpiderModel) create your own Exception classes. The defaultCrawlFile.moog has some malformed URLS you should be able to catch those, like the example:

Handling Exceptions

Java has a robust mechanism for handling programming errors; this is called the exception framework. An exception is a class that contains information about error conditions that have occurred in the software.
There are two main types of exceptions: checked and unchecked. Checked exceptions are caught by the compiler and must be explicitly handled by code. Unchecked exceptions are only detectable at runtime. They may be caught or uncaught. If uncaught, the program will terminate with an error message.

try-catch-finally syntax

Java uses the try - catch - finally syntax to test (ie. try) a section of code and if an error occurs in that region, to trap (ie. catch) the error. Any number of catches can be set up for various exception types. The finally keyword can be used to provide a block of code that is performed regardless of whether an exception is signaled or not. The syntax is:

try
{
// tested statement(s);
}
catch (ExceptionName e1)
{
// trap handler statement(s);
}
catch (ExceptionName e2) // any number of catch statements
{
// display exception to screen
System.out.println("Exception: " + e2);
}
finally
{
// always executed block
}

Create your own Exception Class

In addition to throwing objects whose classes are declared in java.lang, you can throw objects of your own design. To create your own class of throwable objects, you need only declare it as a subclass of some member of the Throwable family. In general, however, the throwable classes you define should extend class Exception.