Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or
#1
Here in code
catch(IndexOutOf BoundsException ex){
System.out.println(“you forget the passing parameters”);
}
we have method catch having the parameter of class IndexOutOfBoundsException ex, here ex is an identifier (or instance of the object)of IndexOutOfBoundsException.
Every object of class has Private ,public, protected parts which can be access public or protected parts with the help of identifier like ex, but in the above method, we are not using the ex to access the any part of object or instance of the class IndexOutOfBoundsException.like ex . anyMethod(getter or setter or other method of the class)
Then if I donot use ex to access any part with help of “.” , then why passing temporary object as a parameter? Why we need it ? where its role in the code?
Reply
#2
Each catch block is an exception handler and handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name.



In this code we are handling IndexOutOf BoundsException , Although we are not using any method of this but as it is required by catch so we are passing this argument.



You can use ex to display details of the exception like



catch(IndexOutOf BoundsException ex){
System.out.println(“you forget the passing parameters”Wink;

System.out.println(ex.getMessage());


}



In simple words we have to pass this argument as this is the requirement, this argument will tell compiler that what type of exception that will handle.

0300-9520262
Get solution in 24 hour
|cpmputer & internetConfusedmileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what is the difference between Static method and the method we write in Clas saneha 1 4,766 04-28-2011, 09:12 PM
Last Post: Vuhelper
  Why getCountStudent() method is called using Class Name instead of first making new saneha 1 5,907 04-28-2011, 09:10 PM
Last Post: Vuhelper
  flush method in java 2011 saneha 1 2,942 04-24-2011, 09:49 PM
Last Post: Vuhelper
  try, catch and finally block? saneha 0 1,992 04-24-2011, 07:26 PM
Last Post: saneha

Forum Jump:


Users browsing this thread: 1 Guest(s)