Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, can Class.forName ever return null?

In Java, can Class.forName ever return null, or will it always throw a ClassNotFoundException or NoClassDefFoundError if the class can't be located?

like image 724
Phil Avatar asked Jan 09 '09 23:01

Phil


2 Answers

Java Docs says it will throw ClassNotFoundException if the class cannot be found so I'd say it never returns null.

like image 127
willcodejavaforfood Avatar answered Sep 28 '22 05:09

willcodejavaforfood


Since null is not mentioned anywhere in the documentation for this method and because there doesn't seem to be any situation in which it would make sense for the method to return null instead of throwing an exception, I think it's pretty safe to assume that it never returns null.

It won't throw a NoClassDefFoundError, but it may throw ClassNotFoundException.

like image 30
Dan Dyer Avatar answered Sep 28 '22 05:09

Dan Dyer