Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading of Exception Classes  RSS feed

 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
As we all know java load classes when they are really needed. But I have seen an exception to this rule

public class A{
public void method1(){
try{
//some code
}catch(MyException e){
}
}

So whenever the "A" is loaded the "MyException" is also loaded even if it is not required in the code path.
I wanted to find out why the special consideration for the classes in the catch block at the time of loading
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to tell us a class named in a catch as a likely Exception isn't "needed"?
 
Manish Singh
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. the exception class in the catch block is not needed unless someone calls "method1".
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Needed' means used in a class that is loaded at the moment, not when a specific method of that class is invoked. If class A references in any place class B, class B is needed to load class A.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish wrote: So whenever the "A" is loaded the "MyException" is also loaded even if it is not required in the code path.

MyException *is* required on the code path (do you mean "classpath" here?), otherwise the code wouldn't compile.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!