Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or

View previous topic View next topic Go down

GMT + 3 Hours Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or

Post by Diya Sun May 29, 2011 4:42 pm

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?
Diya
Diya
Monstars
Monstars

Taurus Goat
Posts : 364
Join date : 2011-02-08
Age : 32
Notworthy

Character sheet
Experience:
Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or Left_bar_bleue10/500Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or Empty_bar_bleue  (10/500)

Back to top Go down

GMT + 3 Hours Re: Why we are passing IndexOutOfBoundsException ex in catch?we have not ex.any Method or

Post by Vuhelper Sun May 29, 2011 4:43 pm

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”);

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.
avatar
Vuhelper
Deep Bench
Deep Bench

Posts : 97
Join date : 2011-05-29

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum