Results 1 to 3 of 3
Thread: Exception question
- 03-11-2009, 07:11 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
Exception question
I want to take a fully qualified class name as the program's argument. Then print out the superclasses of the class.
It says the exception is never thrown. I'm not sure how to check (or write the code) to where I don't need to include a throw statement. I thought if the catch block is within the same method, throw exception is not needed.Java Code:public class Check { public static void main(String[] args) { for(int i=0;i<args.length;i++) { try { Class a = args[i].getClass(); //make a class object while(a.getSuperclass() != null) //loop while it has a superclass { System.out.println(a.getSuperclass().toString() + "is a superclass of " + a.toString()); a = a.getSuperclass(); } } // catch(ClassNotFoundException e) { //compiler says the exception is never thrown here System.out.println("Sorry, that class was not found."); } } } }
- 03-12-2009, 03:24 AM #2
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
I'll assume that you need explaination of the concept voiced in this quote:
"I thought if the catch block is within the same method, throw exception is not needed"
This can be true, however, just making a try{}catch{} statement that you
specify to catch a particular Exception does not mean that code to create
such an Exception will magically appear within the try braces.
For example, change "ClassNotFoundException e" to "Exception e".
This will compile because some sort of exception can be thrown
by the code you put in the try braces.
Also, "NullPointerException e" will compile. You might be familiar
with this exception, and you probably can identify what in the
try braces will cause "NullPointerException" to be thrown.
You have two options:
Find a method that generates "ClassNotFoundException e" and
place it in your try braces.
or
Create your own test that throws "ClassNotFoundException e".Last edited by paul pasciak; 03-12-2009 at 11:42 PM. Reason: two words missing
- 03-12-2009, 06:08 AM #3
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Need help on Exception
By Deon in forum New To JavaReplies: 7Last Post: 02-11-2010, 05:46 PM -
Help with Exception
By bozovilla in forum New To JavaReplies: 2Last Post: 10-19-2008, 05:19 AM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM -
Exception
By Camden in forum New To JavaReplies: 2Last Post: 11-26-2007, 11:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks