Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compilation error

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
Code:
public class ThrowsDemo

   {
      static void throwMethod()
      {

      System.out.println("Inside throwMethod.");

      }

      public static void main(String args[])

           {

           throwMethod();

           }


  }

>javac ThrowsDemo.java // compiled ok

>java ThrowsDemo // run ok

Inside throwMethod

but if i change this code like below.........

Code:
public class ThrowsDemo {  
      static void throwMethod() {  
              System.out.println("Inside throwMethod.");  
               throw new IllegalAccessException("demo");  
      }  
      public static void main(String args[]) {  
           try {  
                 throwMethod();  
           } catch (IllegalAccessException e) {  
                 System.out.println("Caught " + e);  
           }  
       }  
}

it does not even compile. whats wrong ?

a user throwable exception is created in the throwMethod() so that should be caught by the try-catch block.

why there is compilation error ?


 
When you compile your code, the compiler reports this error :

Code:
: unreported exception java.lang.IllegalAccessException; must be caught or declared to be thrown
               throw new IllegalAccessException("demo");

This means that your method "throwMethod()" is throwing an exception, but the method's isgnature does not actaully throw that specific exeption - so you must either (as the compiler says) catch that exception within that specific method, or declare it to be thrown ::

Code:
public class ThrowsDemo {
      static void throwMethod() throws IllegalAccessException {
              System.out.println("Inside throwMethod.");
               throw new IllegalAccessException("demo");
      }
      public static void main(String args[]) {
           try {
                 throwMethod();
           } catch (IllegalAccessException e) {
                 System.out.println("Caught " + e);
           }
       }
}
 
thanks sedj,it is working now. after adding the name of the exception in the signature of the method it is compiling and running.

i have some queries on your comments , i see you have suggested to solve this in two way. right ? i am breaking your comments below

1.so you must either (as the compiler says) catch that exception within that specific method,

2 . or declare it to be thrown ::


and you have given the code which obeys the second category.
how the code will be if it obeys 1st category ?

is it something like below ?...(though it is not working)
i am copying from the console

Code:
public class ThrowsDemo
 {
static void throwMethod()
{
               System.out.println("Inside throwMethod.");
               throw new IllegalAccessException("demo");

                  try{
                       // no code
                  }catch(Exception e)
                   {
                    System.out.println("Danger");
                   }


}



  public static void main(String args[])

                {
              throwMethod();

                }

   }

this is giving error. can you tell me how the code will be if it follows first category ?
 
ohhh..its working
Code:
try{

throw new IllegalAccessException("demo");

  }catch(Exception e)
{
 System.out.println("Danger");
 }
....


probabily this is the first category. i will prefer second category bcoz that is more readable.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top