Solved

exceptions

Posted on 2006-10-29
3
188 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 68
Glassfish admin console not working 1 54
program arguments vs VM arguments 4 25
IBM TS2900 (3572) Tape Autoloader Java? 12 53
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

685 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