3 Replies - 267 Views - Last Post: 16 January 2013 - 04:37 PM Rate Topic: -----

#1 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Throwing custom exceptions

Posted 16 January 2013 - 04:03 PM

Having trouble with some basic code here. We have an array class that lets the array start at variable indexes. I've encountered problems with throwing exceptions. With the add method, the exception for adding to an occupied cell works correctly but I can not get my custom message for out of bounds to show up. I get the generic arrayoutofbounds exception.


class Array<T extends Comparable<? super T>> {
	// Data fields
	private T[] array;
	private int firstIndex, lastIndex, arraySize;

	// Constructor
	public Array(int first, int last) {
		firstIndex = first;
		lastIndex = last;
		arraySize = last - first + 1;
		array = (T[]) new Comparable[arraySize];
	}

// If the index i is out of range, throw an exception with an appropriate
	// message.
	// If there is already an object at the index, throw an exception.
	// Otherwise, add the object x into array at index i.
	public void add(T x, int i) throws Exception {
		if (array[i - firstIndex] == null) {
			if (i >= firstIndex && i <= lastIndex) {
				array[i - firstIndex] = x;
			} else {
				throw new Exception("wsefwef");
			}
		} else
			throw new Exception(i + " is not vacant");

	}

// Print array
	public void print() {
		String string = "";
		String label = "";
		for (int i = 0; i < arraySize; i++) {
			string += "[ " + array[i] + " ]";
		}
		System.out.println(string);
		System.out.println(label);
	}
}





class Prog1 {
	public static void main(String[] args) {
		Array<Integer> array = new Array<Integer>(3, 7);
		// Array indices are 3, 4, 5, 6, and 7

		// Testing of methods from Array using array
		System.out.println("Array at the Start");
		array.print();
		System.out.println();
		
		//Add method
		
		System.out.println("Test add Method");
		System.out.println( "Put value 3 in 5 index");
		try {
			array.add(3,5);
			System.out.println("Success");
		} catch (Exception e) {
			
			System.out.println(e);
		}
		array.print();
System.out.println( "Put value 3 in 5 index again( already full");
		try {
			array.add(3,5);
			System.out.println("Success");
		} catch (Exception e) {
			
			System.out.println(e);
		}
		array.print();
		
		System.out.println( "Put value 3 in 10 index( Out of Bounds ");
		try {
			array.add(3,10);
			System.out.println("Success");
		} catch (Exception e1) {
			
			System.out.println(e1.getMessage());
		}
		array.print();
}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Throwing custom exceptions

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Throwing custom exceptions

Posted 16 January 2013 - 04:17 PM

View Postjjallenjj, on 16 January 2013 - 07:03 PM, said:

but I can not get my custom message for out of bounds to show up. I get the generic arrayoutofbounds exception.

Probably because this is what happens
Show us the stack trace I'll bet it does not happen where you throw your own Exception

When you do

if (array[i - firstIndex] == null)

if i == 1000 you are testing if array[1000-3] .... array out of bounds

You should check the index before testing the array :)/>

This post has been edited by pbl: 16 January 2013 - 04:18 PM

Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,204
  • Joined: 20-September 08

Re: Throwing custom exceptions

Posted 16 January 2013 - 04:26 PM

Quote

but I can not get my custom message for out of bounds to show up
That's because you don't catch the Exception yourself before the jvm. You have two alternatives:

a. do bounds checking as pbl said and throw your custom Exception
b. catch the jvm Exception and rethrow a custom
btw

Quote

		Array<Integer> array = new Array<Integer>(3, 7);
		// Array indices are 3, 4, 5, 6, and 7


That's not the case. Array indices are 0..4

This post has been edited by g00se: 16 January 2013 - 04:29 PM
Reason for edit:: Clarification

Was This Post Helpful? 1
  • +
  • -

#4 jjallenjj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 07-May 11

Re: Throwing custom exceptions

Posted 16 January 2013 - 04:37 PM

You got it guys, thanks. Wish I didn't spend an hours before posting it on here

Goose yes I get that, but those indicse would be those of the UI, not the actual array

This post has been edited by jjallenjj: 16 January 2013 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1