I have two options (which technically are the same, as I understand) to declare a custom exception class thrown only from a particular class com.XXX.Foo
:
com.XXX.CustomException
com.XXX.Foo.CustomException
Which option is better?
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.
Java inner class is defined inside the body of another class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.
In Java programming, nested and inner classes often go hand in hand. A class that is defined within another class is called a nested class. An inner class, on the other hand, is a non-static type, a particular specimen of a nested class.
In case the exception is very specific to Foo
class, I don't mind keeping it as a public
Nested class. And whenever it seems like a time to extract that out, simply extract that out.
In general practice, I have never seen any nested class defined for Exception, though. I also don't know if one exist in Java API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With