views:

472

answers:

6

Is it possible to catch a method in the current class the try-catch block is running on? for example:

  public static void arrayOutOfBoundsException(){
      System.out.println("Array out of bounds");
  }

    .....

  public static void doingSomething(){
    try
    {
       if(something[i] >= something_else);
    }
    catch (arrayOutOfBoundsException e)
    {
       System.out.println("Method Halted!, continuing doing the next thing");
    }
  }

If this is possible how will it be the correct way to call the catch method?

If this is not possible, could anyone point me in the right direction, of how to stop an exception from halting my program execution in Java without having to create any new classes in the package, or fixing the code that produces ArrayOutOfBoundsException error.

Thanks in Advance,

A Java Rookie

+1  A: 

I think you mean to have the arrayOutOfBoundsException method throw the exception caused... If i understand correctly:

public static void arrayOutOfBoundsExceptionMethod() throws ArrayIndexOutOfBoundsException  {
      \\...
      \\ code that throws the exception
  }

    .....

  public static void doingSomething(){
    try
    {
       if(something[i] >= something_else);
       arrayOutOfBoundsExceptionMethod();
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
       System.out.println("Method Halted!, continuing doing the next thing");
    }
  }

Is that what you are asking?

Savvas Dalkitsis
There's no ArrayOutOfBoundsException :) But there is ArrayIndexOutOfBoundsException, which is what gets thrown.
Chris Dennett
oh come on i just copied of his code... I didn't even pay attention to spelling :P
Savvas Dalkitsis
+3  A: 

What you are wanting to do is handle an Exception.

public static void doingSomething(){
    try {
        if (something[i] >= something_else) { ... }
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Method Halted!, continuing doing the next thing");
    }
}

That's all you need. No extra classes, no extra methods.

An Exception is a special type of class that can be "thrown" (you can throw it yourself by using the throw keyword, or Java may throw one for you if, for example, you try to access an array index that does not exist or try to perform some operation on a null). A thrown exception will "unwrap" your call stack ("escaping" from each function call) until your program finally terminates. Unless you catch it, which is exactly what the syntax above does.

So if you were writing a function a() that called a function b() that called a function c() and c() threw an exception, but the exception was not caught in b() or c(), you could still catch it in a():

void a() {
    try {
        b();
    catch (SomeExceptionClass e) {
        // Handle
    }
}

That said, if it is possible to prevent an exception from being thrown in the first place, that is often a better idea. In your particular case, this would be possible since all arrays in Java know their own length:

public static void doingSomething(){
    if (i >= something.length) {
        System.out.println("Method Halted!, continuing doing the next thing");
    } else {
        if (something[i] >= something_else) { ... }
    }
}
Adam Batkin
Thanks a lot for your answer, Im not in the position to modify the code causing the error as I am evaluating to check whether it was written right or wrong.
Carlucho
I think I see what you mean. I've added a bit in the middle to explain how you can still catch the exception just by wrapping the function call itself in a try/catch block.
Adam Batkin
Great, That seems to be working very well. Thanks a lot buddy. Ur explanation was very useful not only to solve the problem but to understand how it actually works.
Carlucho
+3  A: 

Hmm.. I think you are a little bit confused. You don't catch a method with try catch blocks. You catch Exceptions. Exceptions are more like things.

This alone (note the capital "A" of ArrayOutOfBoundsException) will prevent your program to terminate even if that Exception is thrown. You don't need to declare a "arrayOutOfBoundsException()" method.

public static void doingSomething(){
    try
    {
       if(something[i] >= something_else);
    }
    catch (ArrayOutOfBoundsException e)
    {
       System.out.println("Method Halted!, continuing doing the next thing");
    }
  }

Note the little "e"? That's the reference you can use to refer to this Exception. So, you can ask things to this Exceptions using this little local variable. For example, if you want to print where the exception happened, you can do e.printStackTrace(); .

Enno Shioji
right i actually tried that but netbeans is telling me that i need to create a class "ArrayOutOfBoundsException" in the package, and i dont really want a class to be created, any thoughts?Thanks a lot for your answer, you are very kind
Carlucho
The name is 'ArrayIndexOutOfBoundsException' - you're just missing the 'Index' part.
Shakedown
Thanks buddy! All the best
Carlucho
A: 

I'm trying to understand what you're asking, your terminology is wrong in places :) If you try and set an index out of bounds, you'll get a built-in exception anyway, ArrayIndexOutOfBoundsException. You can catch this and use this inside the method, or throw it using a class XX throws ArrayIndexOutOfBoundsException declaration in the header of the method, so that the invoking method can catch it (its not required to catch an ArrayIndexOutOfBoundsException as it's a RuntimeException -- i.e., nothing happens if it goes uncaught, it's unchecked.

See this wiki article for more details: http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions.

Chris Dennett
Thanks for your answer i give the wiki a read!
Carlucho
+2  A: 

You catch Exceptions, with a try-catch block. Not sure if you can catch methods with this.

public static void myMethod(){
    try {
        // check for some given condition
    }
    catch (conditionNotSatisfied e) {
        System.out.println("Condition was not satisfied, you tried to do something which is not allowed");
    }
}

The 'e' in the catch() is the reference used for referring to an Exception. Exception refers to this when it wishes to check your given condition and return you a warning or error about the condition not getting satisfied.

gagneet
Thanks for sharing your knowledge buddy ill certainly keep your reply in mind but i'm going with Adam's reply for now. Take it easy
Carlucho
not at all a issue, i think he has addressed the issue very well :-)
gagneet
+1  A: 

It is not clear what you mean by "Is it possible to catch a method". In your case, you should write your code as :

   if(i < something.length && something[i] >= something_else);

No ArrayOutOfBoundsException is needed.

fastcodejava