Solved

exceptions

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

813 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

11 Experts available now in Live!

Get 1:1 Help Now