Solved

Creating a Class that throws IndexOutOfBoundsException

Posted on 2001-06-15
5
203 Views
Last Modified: 2010-04-16
Hello

I was wondering if someone can help me create a class that throws an IndexOutOfBoundsException

i.e. like
class ThrowingExcep{
public Object elementAt (int index)throws IndexOutOfBoundsException{
return ___?____   ;}
}

I do not know what to put in the return statement

Thanks in advance
Will
0
Comment
Question by:willcont
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 50 total points
ID: 6195548
What you want is a method that throws the exception, not a class (classes don't throw exceptions). The answer is that you don't need a return statement if you are planning to unconditionally throwing an exception. What you would do is simply this:

public Object elementAt(int i){
  throw new IndexOutOfBoundsException(String.valueOf(i));
}


A more reasonable elementAt method would be this (class code included):

public class Vector{

  private Object [] arr;
  private int size;

  public Object elementAt(int i) throws IndexOutOfBoundsException{
    if ((i<0)||(i>=size))
      throw new IndexOutOfBoundsException(String.valueOf(i));
    return arr[i];
  }

}


A throw statement passes the control to the next enclosing catch statement - if you have no such statement in the method that throws the exception, the method terminates without returning a value and control is tranferred (recursively up the stack) to the enclosing catch statement of the method that called your method.

Sasha Maryanovsky.
0
 
LVL 16

Expert Comment

by:imladris
ID: 6195551
If I understand the intent correctly, the exception would be thrown when something went wrong (and no return would be needed), otherwise you return the relevant Object. So I think it would look something like:

class ThrowingExcep{
{   Vector l;
    int max;

    ThrowingExcep()
    {   l=new Vector();
        max=0;
        return;
    }

    public Object elementAt(int index) throws IndexOutOfBoundsException
    {   if(index>=max)throw new OutOfBoundsException();
        return(l.elementAt(index));
    }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6195565
import java.util.*;

class StringStore{

     Vector v;

     public StringStore(){
           v = new Vector();
     }

     public void putString(String s){
          v.add(s);
     }

     public String getString(int index) throws IndexOutOfBoundsException{
          if (index > v.size()){
               throw new IndexOutOfBoundsException();
          }
          return (String)v.elementAt(index);
     }
}
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 6195567
Woah...everyone posted at once.
Should have reloaded before I hit submit.
0
 

Author Comment

by:willcont
ID: 6195572
Thank you
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question