3

i was trying to run the following code but i am getting error please clarify my doubt

import java.util.*;

    class Except 
    { public class AlfaException extends Exception{}
      public static void main(String[] args)
       {

        int b;
        Scanner s=new Scanner(System.in);
          try
            { 
              b=s.nextInt();
            }
          catch(InputMismatchException ex)
               {
                 try
                     {
                        if(('b'> 67)&&('b'<83)){}
                     }
                 catch(AlfaException e){
                   throw new AlfaException("hello");
                    }         
                 System.out.println("error found");
               }
        }
    }  



 Except.java:20: non-static variable this cannot be referenced from a static cont
ext
               throw new AlfaException("hello");
                     ^

1 error

3
  • I wonder why I cannot edit or even retag this question Commented Jul 19, 2011 at 6:46
  • @eng Because someone with a reputation lower than 2000 has already edited the question and the edit hasn't been approved by users with a higher reputation. Commented Jul 19, 2011 at 6:53
  • Alternatively, you can throw the exception as follows: new Except().new AlphaException();. Commented Jul 19, 2011 at 6:55

4 Answers 4

5

Static context is a context that runs on class without actual instance of that class. Your main method is static, that means it can only access static variables. However, your AlfaException is not static. Meaning that it will be bound to an instance of Except class - which you do not have.

Therefore you have 2 choises:

  1. Make AlfaException also static: public static class AlfaException extends Exception{}. That will make it reside in static scope so it will be possible to access it from static functions.
  2. Move all the main(...) method logic into non-static context. Create a function called doWork() that is not static, move all the code from main to doWork, and then call it like this:

.

public static void main(String[] args) {
    Except instance = new Except();
    instance.doWork();
}
Sign up to request clarification or add additional context in comments.

Comments

3

Your AlfaException is a non-static inner class of Except, so it can only be instantiated from inside an instance of Except. The main method is static, so doesn't have an enclosing instance.

Change the declaration of AlfaException to:

public static class AlfaException extends Exception{}

and it should work.

Comments

0

There are couple of mistakes:

  1. AlfaException is never thrown in your try-block
  2. AlfaException is a non-static inner class of Except (see other answers)
  3. If you rethrow AlfaException in catch-block, the main needs a throws like this:

    public static void main(String[] args) throws AlfaException { ...
    

Comments

0
import java.util.*;

class Except 
{ public static class AlfaException extends Exception{

    public AlfaException(String string) {
        super();
    }
}
  public static void main(String[] args) throws AlfaException
   {

    int b;
    Scanner s=new Scanner(System.in);
      try
        { 
          System.out.println("Enter the value for b");
          b=s.nextInt();
          System.out.println("b value is "+b);
        }
      catch(InputMismatchException ex)
           {
             if(('b'> 67)&&('b'<83)){}         
             System.out.println("error found");
           }
    }
}

output:
Enter the value for b
80b
error found

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.