0

If I have a try/catch block which catches an IndexOutOfBounds exception, but in the same code, I want to access a file, is it possible to do a general excepction which could catch an IOException too?

Normally when I use try/catch I just put the exception type, but I have never tried it before. Is it wrong to do so or just bad practice?

try {
    // code to access a File which does not exist and inserts into an array index which does not exist
} catch (Exception eitherException){
    // prints "An exception happened"
}

Would I need nested blocks, multiple catch clauses or is this ok? I have researched online for a while but cannot find an answer. Thank you!

  • https://www.journaldev.com/629/java-catch-multiple-exceptions-rethrow-exception – SSK Jun 08 '20 at 14:53
  • 1
    I would suggest that you probably should be using two try catch blocks as it sounds to my like they are separate concerns within the method. This way you will end up with more readable code that is easier to debug. If the `IOException` relates to files are any other `Closable` resource consider a try-with-resources – Gavin Jun 08 '20 at 14:53
  • 3
    Whenever you run into a situation where you think something might work, don't let "I have never tried it before" stop you. The worst thing that can happen is that you spent some time learning something and have to back out some code. The best thing that can happen is that you learn something and answer your question. – MarsAtomic Jun 08 '20 at 14:54
  • 2
    Why do you need a try-catch around an IndexOutOfBoundsException? In my opinion, if you know this can happen, it is better to check for the condition and take actuion before it happens. – ControlAltDel Jun 08 '20 at 14:55
  • 1
    I agree with ControlAltDel, you shouldn't be getting the `IndexOutOfBoundsException` in the first place, just the `IOException`. Catching exceptions is more expensive than checking the length of your array. – user Jun 08 '20 at 15:10
  • 1
    Thanks for the advice guys. I agree the IndexOutOfBoundsException should not happen, but it was the only exception I could think of at the time – Ronnie Lightweightbaby Coleman Jun 08 '20 at 15:17

2 Answers2

2

The code you have written will catch IOException too since Exception class is the parent class of all exceptions. It would catch all types of exception. If you do not want to catch all exceptions and just these two exceptions , you can write like this.

try {
// code to access a File which does not exist and inserts into an array index which does not exist
} catch (IndexOutOfBounds | IOException  eitherException){
// prints "An exception happened"
}
2

It depends on how you want to handle the different exception thrown. You may want different functional flow during different exceptions thrown (case 1) or have a same function flow during exception (case 2) or just ignore the exception altogether and log it (case 2). Ex: Say I have an application that loads a file externally. In case an IO exception is thrown I want to use the default properties defined in the code itself. So I can use case 1 and do the required when IOException is thrown etc.. and when IndexOutOfBounds is thrown some other logic get executed.

Case 1:

try {
// business logic
} catch (IndexOutOfBounds exceptionOne) {
// print log as index out of bounds exception thrown
// execute some business logic
} catch (IOException excpetionTwo) {
// print log as IO exception thrown
// execute some other business logic
}

Case 2:

try {
// business logic
} catch (IndexOutOfBounds | IOException exception) {
// print log as exception thrown
// execute business logic
}
Vaibhav
  • 116
  • 1
  • 7