java - Trying to throw own exception -


i created own throwable exception when want throw it, editor says, reference enclosing class required. don't know, need write.

here's code:

public class main {     int = 0;     public main() {         if (i == 0) throw new myexception("i must not 0"); //here says enclosing class     }     public static void main(string[] args) throws exception {         new main();     }     public class myexception extends exception {         public myexception(string e) {             super(e);         }     } } 

someone can tell me, , must write?

you've defined myexception inner class of main, created instance of no corresponding instance of main available (since main method static method).

you need declare exception class separately, outside of main. changing access public package-private let keep declaration in same file. otherwise, since can have 1 public class per file, need go in own file.

alternatively can define static inner class, so:

public class main {     int = 0;     public static void main(string[] args) throws exception {         if (i == 0) throw new myexception("i must not 0"); //here says enclosing class     }      static class myexception extends exception {         public myexception(string e) {             super(e);         }     } } 

making class static means not refer instance of enclosing class.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -