Results 1 to 3 of 3
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Exception question

    I want to take a fully qualified class name as the program's argument. Then print out the superclasses of the class.

    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.");
    			}
    				
    			}		
    		
    	}
    
    }
    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.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    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

  3. #3
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Try declaring a class that does not exist

    example:

    IdoNotExist thiswontwork = new IdoNotExist(); //this shoud generate a class not foud exemption (i hope)
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 05:46 PM
  3. Help with Exception
    By bozovilla in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 05:19 AM
  4. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 11:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •