Solved

exceptions

Posted on 2006-10-29
3
191 Views
Last Modified: 2010-03-31
Hi,

How can I throw an exception I define in java? I would like something like:

    int GetIndex(int nIndex)
    {

        if (nIndex > list.GetCount()) {
             throw ?;
        }
    }

    void Something()
    {
         try {
             list.GetIndex(88888);
         }
         catch (?) {
              print('bad index sadly.");
         }
    }

yeah that's just pseudocode,

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[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
  • 2
3 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17831747
   int GetIndex(int nIndex) throws Exception
    {

        if (nIndex > list.GetCount()) {
             throw new Exception("bad index sadly.");
        }
    }

    void Something()
    {
         try {
             list.GetIndex(88888);
         }
         catch (Exception e) {
              e.printStackTrace()
              print(e.getMessage());
         }
    }
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17832105
Assuming you have defined your exception class, you need to declare it to be thrown by the method:

int GetIndex(int nIndex) throws MyExceptionSubclass
{

        if (nIndex > list.GetCount()) {
             throw new MyExceptionSubclass("Index out of range");
        }
}

In that case, though, you would normally allow the runtime to throw an exception for those reasons
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17848131
:-)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

732 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