Are Java exceptions deserving of their own file?

Whenever I define a new exception, I tend to do one of two things:

  • Create an exception file containing a list of all the new exceptions defined in my project.
  • Put each specific exception in the same file as the class in which it was thrown.

Netbeans do not like it when I have a public exception defined in the same file as another class, for example, in paragraph 2. Although I have not tried this yet, I believe that it will yell at me for similar reasons for having more than one exceptions in the same file as in paragraph 1.

Should each exception really have its own file, or is there some other style that Netbeans will accept? I am new to Java and Netbeans and would like to hear what the appropriate style is.

+3
source share
3 answers

I put exceptions in my own file. Why not?

You cannot have more than one open class in one file. If you are trying to put a public exception in another public class under the name of some kind of efficiency, I would recommend going back and re-reading the Java specification.

+9
source

The exception is a class and the same treatment is required as all other classes.

, , ( , ), . .

, Java .

, Java . .

+5

( ). , A , B. , , .

- , .

, , . - , , , . , , , .

+2

Source: https://habr.com/ru/post/1768724/


All Articles