Usually you find an exception in a method when you want your program to continue to work. You throw an exception if you want a higher level method to call this method to handle the exception. For example, you can completely drop it to your Main method, which has a try..catch block (probably with different catch blocks for different exceptions), encapsulating all your method calls, and exceptions can be handled there (for example, program termination).
Remember that throwing an exception will immediately terminate the method. This affects the flow of your code. If you can have an exception in the middle of the method, and the code below cannot work, if this exception occurs, you need to either wrap the entire section in a try / catch block or throw an exception.
The general word of advice is printStackTrace () is bad. You can create the best error output yourself (and you can enable stack tracing as well as your output). Better yet, use logging.
I recommend reading this introduction to exceptions and this article, which describes the good and bad exception patterns .
source share