Solved

exceptions

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now