2016-11-01

posted Nov 1, 2016, 6:01 AM by Samuel Konstantinovich   [ updated Nov 1, 2016, 6:01 AM ]
Goal Exceptions!!!!!

Checked Exceptions: Are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword. (We have not seen this yet! Don't worry about it for now)
For example, a Java program that opens file throws a checked exception FileNotFoundException, which must be caught inside the program. 


Unchecked Exceptions: Many exceptions are not checked at compiled time, these are the ones you are familiar with at this point. It is up to the programmers to specify or catch the exceptions, if not then they will crash the program and print the stack trace. In Java exceptions that are subclasses of Error and RuntimeException are unchecked exceptions.

IndexOutOfBound and IllegalArgumentException are a subclasses of RuntimeException. You don't have to memorize which are checked and unchecked. But you should understand the difference, and know what to do if you come across a checked exception when writing code. (Like with scanning a file)

Links to some unchecked exceptions:
https://docs.oracle.com/javase/7/docs/api/java/lang/IndexOutOfBoundsException.html
http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html

Generally you check for some condition before throwing an exception.
You can only throw exceptions that exist, unless you write your own.

//inside the shoot method of a Ship (or it's subclass) you might have something like this:
    if( isDeleted() ){
       throw new DeadShipsCannotShootException();
    }

//Inside a StackOfPancakes class:
public Pancake eat() {
    if (size == 0) {
        throw new EmptyStackException();
    }
    return getNextPancake();
}




Add/Modify the following Methods to your SuperArray

public SuperArray(int initialCapacity)
     Constructs an empty list with the specified initial capacity.
Parameters:
     initialCapacity - the initial capacity of the list
Throws:
     IllegalArgumentException - if the specified initial capacity is negative


get(index)
Parameters:
  index - index of the element to return
Returns:
  the element at the specified position in this list
Throws:
  IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())


set(index,value)
Parameters:
   index - index of the element to replace
element - element to be stored at the specified position
Returns:
   the element previously at the specified position
Throws:
   IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())


add(index,element)
Parameters:
    index - index at which the specified element is to be inserted
    element - element to be inserted
Throws:
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())


remove(index)
Parameters:
   index - the index of the element to be removed
Returns:
    the element that was removed from the list
Throws:
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
Comments