• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple catch blocks  RSS feed

 
sonalika kulkarni
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

We know that we should have multiple catch blocks with specific catches the code can throw. I have 1 simple query
say we are working with indexes and we can have either ArrayIndexOutOfBoundsException or
StringIndexOutOfBoundsException then why cannot we catch them by saying 'IndexOutOfBoundsException'. If say in both the cases we are printing the error to log.
In general if we say why we should have multiple catches if same catch can be caught by super class.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sonalika kulkarni wrote:... we can have either ArrayIndexOutOfBoundsException or
StringIndexOutOfBoundsException then why cannot we catch them by saying 'IndexOutOfBoundsException'.

You can do that. Did you try? Did you get an error message? What does your code look like?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
multiple catch block gives us more specific information for any exception...if we put super class then we lost that information...so multiple catch is good for real life problems if we want more related information about any exception...you got it.
 
sonalika kulkarni
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i tried that
public class Test {
public static void main(String[] args) {
throeException();
}

private static void throeException() {
int arr[] = new int[5];
try {
System.out.println(arr[22]);
} catch (IndexOutOfBoundsException e){
System.out.println("caugh t: "+e);
}
}
}
it gives : caugh t: java.lang.ArrayIndexOutOfBoundsException: 22

so it means that i can get the information of the exception by catching it in any super type of exception. So how does it matter if i caught it in IndexOutOfBoundsException or ArrayIndexOutOfBoundsException
 
sonalika kulkarni
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thakur Sachin Singh wrote:multiple catch block gives us more specific information for any exception...if we put super class then we lost that information...so multiple catch is good for real life problems if we want more related information about any exception...you got it.

I that could be the case but not always say we are using like IO operation then it will be helpful to catch specific exception or we are working for date formatting. But in case of we are just logging the exception then also we should go for catching specific exceptions?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes this is matter if you put child class then it gives real exception generated by the compiler.
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not got it...make me clear..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.

sonalika kulkarni wrote:so it means that i can get the information of the exception by catching it in any super type of exception. So how does it matter if i caught it in IndexOutOfBoundsException or ArrayIndexOutOfBoundsException

I don't understand what you mean here. Sometimes you want to catch the exceptions in separate catch blocks, because you want to handle those exceptions differently. And other times you want to handle them the same way, so if they have a common superclass (such as IndexOutOfBoundsException in your example) you can just catch the superclass exception. What is most appropriate depends on the situation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!