0

Example1Exception and Example1Method belong together in the same file. It would not make sense to put them in separate files.

public class Example1
{
    public class Example1Exception extends Exception
    {
        public Example1Exception(String message)
        {
            super(message);
        }
    }

    public static void Example1Method() throws Example1Exception
    {
        throw new Example1Exception("hello"); //error: non-static variable this cannot be referenced from a static context
    }
}

How can I throw Example1Exception in Example1Method?

cja
  • 9,512
  • 21
  • 75
  • 129
  • 1
    Use an IDE like Eclipse or NetBean. That will help a lot with the java syntax. – K.C. Jan 17 '14 at 13:56
  • 1
    It would really help if you would post code that you've compiled, so that the error you're claiming matches the real one. Your declaration of Example1Exception isn't a proper class declaration, and your Example1Method method declaration is missing a return type and parameter list. This sort of thing distracts from the real issue. – Jon Skeet Jan 17 '14 at 14:08
  • You'd have liked my real code even less :) – cja Jan 17 '14 at 14:57
  • @cja: I didn't say you needed to post your original code. It's *great* that you tried to write a short but complete example demonstrating the problem. It's not great that you didn't try to compile it before posting it, to make sure that it actually *did* demonstrate the same problem you're facing in your real code. – Jon Skeet Jan 17 '14 at 15:51

3 Answers3

11

(Assuming you actually declare Example1Exception using a class declaration..., and that the method declaration is fixed too...)

Example1Exception is an inner class - it needs a reference to an enclosing instance of the outer class.

Options:

  • Provide it with a reference (but why?)
  • Make it a nested (but non-inner) class by changing the declaration to include static
  • Make it a top-level class

Personally I'd usually go for the last option - why do you want it to be a nested class anyway? Why would it not make sense to put them in separate files? What do you gain by having it as a nested class, other than a bunch of complexity? Do you really want people to declare catch (Example1.Example1Exception ex) { ... }

If you really want it to be nested, you probably just want it to be a non-inner class - you're not using the implicitly-required reference to an instance of Example1.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I think exception classes should be public. A file can contain just one public class (according to http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file). So I can't declare several exception classes in one file. So either I end up with a load of small files each containing one exception or I go for the first or second option. – cja Jan 17 '14 at 15:17
  • @cja: What is the *harm* in having several small files each containing a single exception? Are you really declaring *so* many different exceptions that it's a problem for you? If so, you should consider whether or not that's really helpful... – Jon Skeet Jan 17 '14 at 15:20
  • Reduced browsability. You're reading a newspaper. You want every article on its own page or does it make sense to group all the brief minor crime reports together? – cja Jan 17 '14 at 15:47
  • @cja: I would say you're reducing the readability for your *users* by nesting classes just on the grounds that you don't like having multiple small files. I also don't think it reduces the readability for your code either... Classes are not (primarily) intended for the purpose of grouping other types: packages are. Anyway, I've given my opinion - if you want to make them all nested (non-inner) classes then that's your choice. – Jon Skeet Jan 17 '14 at 15:49
0

Make the exception class static:

public static class Example1Exception extends Exception
{
    public Example1Exception(String message)
    {
        super(message);
    }
}
Tim B
  • 40,716
  • 16
  • 83
  • 128
0

If you really want to do it like that, this is code :

public class Example1 {
    public class Example1Exception extends Exception {
        public Example1Exception(String message) {
            super(message);
        }
    }

    public static void Example1Method() throws Example1Exception {
        throw new Example1().new Example1Exception("hello");
    }
}

But like someone else already said: it makes not much sense to declare the Exception as an inner class.

K.C.
  • 2,084
  • 2
  • 25
  • 38