?
Solved

exceptions

Posted on 2006-10-29
3
Medium Priority
?
193 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 2000 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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