Good day, dear Habr.
I would like to tell you how to properly use exceptions in Java. Part of this material is viewed on the Internet, and is also covered a little in the book
J.Bloch Effective Java . It is about using checked and unchecked (checked / unchecked) exceptions. The article will be useful for beginners, because At first, it is not always clear how to properly use exceptions.
The exception hierarchy in Java is represented as follows: the parent class for all Throwable. It inherited 2 classes: Exception and Error. From the class Exception inherited more RuntimeException.
Error - critical errors that may occur in the system (for example, StackOverflowError). As a rule, their system is processed. If they occur, the application closes, as in this situation, the work can not be continued.
Exception is a checked exception. This means that if a method throws an exception that inherits from Exception (for example, IOException), then this method must be enclosed in a try-catch block. The method that throws the exception must contain the throws construct in the signature. Checked (checked) exceptions mean that the exception could have been foreseen and, accordingly, it should be processed, the application should continue to work. An example of such an exception is an attempt to create a new file that already exists (IOException). In this case, the application should continue to work and the user should receive a notification for what reason the file could not be created.
For example:
try { File.createTempFile("prefix", ""); } catch (IOException e) {
In this example, you can see that the createTempFile method can throw an IOException when the file cannot be created. And this exception must be handled accordingly. If you try to call this method outside a try-catch block, the compiler will generate an error and you will be offered 2 correction options: surround the method with a try-catch block or the method inside which File.createTempFile is called should throw an IOException (to pass it to the top level for processing).
')
RuntimeException are unchecked exceptions. They occur during the execution of the application. These exceptions include, for example, NullPointerException. They do not require the mandatory conclusion in a try-catch block. When a RuntimeException occurs, it indicates an error made by the programmer (uninitialized object, going outside the array, etc.). Therefore, this exception does not need to be processed, but it is necessary to correct the error in the code so that the exception does not occur again.
Below is an example of how to work correctly with RuntimeException:
public int calculateSquare(Rectange rect) { if (rect == null) { throw new NullPointerException(“Rectangle can't be null”); } // calculate rectangle square int rectWidth = rect .getWidth(); int rectHeight = rect .getHeight(); int square rectWidth * rectHeight(); return square; } ... Rectangle rect = new Rectangle(); int square = calculateSquare(rect); ….
In this example, the method accepts an object of the Rectangle class. The method description contains the string @throws, which describes an exception that can be thrown and under what conditions. However, the method signature does not contain a throws construct. This means that when calling a method, it is not necessary to wrap it with a try-catch block. A programmer must prevent the transfer of an uninitialized object to the method.
I hope such small explanations can sort out a little when it’s time to handle exceptions, and when you just have to correct errors in the code.