Welcome To Java Exceptions !
(The way to avoid system crashes...)

Exceptions and Errors, Instructor Jeff Zhuk

Exceptions signal abnormal conditions that should be specially handled to prevent program termination. You can throw exceptions if you detect an error condition. You can also catch and handle exceptions. RuntimeException and all of its subclasses do not have to be caught or declared in a throws clause of a methods. All other exceptions must be caught or declared in the throws clause.

You can create your own exception type by extending any of the exception classes. This allows you to create more specific exceptions that may explain a specific condition better.

Errors generally signal that a non-recoverable error has occurred. Errors should not be caught.

Using and Creating Exceptions

/** 
* Example of throwing an Exception 
**/ 
	public void vectorChecker(Vector iVector)
	  throws IndexOutOfBoundsException 
	{ 
		if (x >= iVector.size())
			throw new IndexOutOfBoundsException(
			"\nOut Of Vector Size=" + iVector.size() + " x=" + x);
	}

/** 
* Example of handling an Exception 

**/ public void catchException() { try { vectorChecker(_aVector); } catch (IndexOutOfBoundsException ibe) { System.out.println(ibe); } catch (Exception e) { System.out.println("failed: " + e); } }

Back To Java School