-1

I am getting the following error when I try to compile this code, which is supposed to create a custom Java array that doesn't use generics. I am pretty sure this is down to not creating the array properly, but I'm not sure.

Any help would be really appreciated! Thanks!

Current Compile Error Excerpt:

51: error: unreported exception Exception; must be caught or declared to be thrown strList.add("str1");

Custom Array Class:

public class MyList {

Object[] data; // list itself. null values at the end
int capacity; // maximum capacity of the list
int num; // current size of the list
static final int DEFAULT_CAPACITY = 100;

public MyList() {
    this(DEFAULT_CAPACITY); // call MyList(capacity).
}
public MyList(int capacity) {
    this.capacity = capacity;
    data = new Object[capacity]; // null array
    num = 0;
}
public void add(Object a) throws Exception {
    if (num == capacity) {
        throw new Exception("list capacity exceeded");
    }
    data[num] = a;
    num++;
}
public Object get(int index) {
    // find the element at given index
    if (index < 0 || index >= num) {
        throw new RuntimeException("index out of bounds");
    }
    return data[index];
}
public void deleteLastElement() {
    // delete the last element from the list
    // fill in the code in the class.
    if (num == 0) {
        throw new RuntimeException("list is empty: cannot delete");
    }
    num--;
    data[num] = null;
}
public void deleteFirstElement() {
    // delete first element from the list
    for (int i = 0; i < num - 1; i++) {
        data[i] = data[i + 1];
    }
    data[num - 1] = null;
    num--; // IMPORTANT. Re-establish invariant
}


public static void main(String[] args) {
    MyList strList = new MyList();
    strList.add("str1");
    strList.add("str2");
    System.out.println("after adding elements size =" + strList);
}


}
hardanger
  • 2,349
  • 1
  • 16
  • 23

1 Answers1

0

You need to declare that main throws can exceptions by doing this:

public static void main(String[] args) throws Exception{
...

or

putting the strList.add(...) in a try-catch block:

...
try{
    strList.add("str1");
    strList.add("str2");
} catch(Exception e){ 
    e.printStackTrace();
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
  • 1
    You never need to declare `throws RuntimeException`, I think you mean `throws Exception`. – Mark Rotteveel Nov 12 '16 at 14:14
  • Both work. but for the sake of generality, `throws Exception` is better. – ItamarG3 Nov 12 '16 at 14:15
  • 1
    and catching the RuntimeException will not fix the compiler error in this case either – luk2302 Nov 12 '16 at 14:15
  • 1
    And no, *"Both work"* is wrong, **only** what @MarkRotteveel proposes works – luk2302 Nov 12 '16 at 14:16
  • @luk2302, Again, I got confused with two different methods in the question code. – ItamarG3 Nov 12 '16 at 14:16
  • 1
    Eagerness to help or to farm rep-points? I saw three answers from you in the past minutes and all of them were flawed. Don't try to be the ["fastest gun in the west"](http://meta.stackexchange.com/questions/9731/fastest-gun-in-the-west-problem) and recheck your answers before posting. Also do a minimal effort to search for a duplicate, before answering. – Tom Nov 12 '16 at 14:21
  • Thanks so much MarkRotteveel, itamargreen and luk2302 - works perfectly and amazingly fast response time! – hardanger Nov 12 '16 at 14:23
  • Call me crazy but isn't the proper fix here to make the add exception an unchecked exception (such as RuntimeException)? This would only happen when the collection is improperly used. Either that or grow the size of the collection. – candied_orange Nov 12 '16 at 14:24