Why is exception handling useful in java




















That allows the caller of your class to implement the handling as it fits the current use case. I will get into more details about best practices and common errors in future posts of this series.

You should use a checked exception for all exceptional events that can be expected and handled by the application. You need to decide if you want to handle it within a method or if you specify it. You can handle it with a try-catch-finally or a try-with-resource block. If you decide to specify the exception, it becomes part of the method definition, and the exception needs to be specified or handled by all calling methods.

You are not required to handle or specify this kind of exception, but you can do it in the same way as you handle or specify a checked exception.

When using Retrace APM with code profiling, you can collect exceptions directly from Java, without any code changes! Prefix works with. Click here to read more about the acquisition. Try Our Free Code Profiler. Try Our Code Profiler.

By Role. By Technology. By Language. Documentation Support Ideas Portal Menu. Start Free Trial. About the Author Latest Posts. About Thorben Janssen Thorben is an independent trainer and author of the Amazon bestselling book Hibernate Tips - More than 70 solutions to common Hibernate problems.

What is Cloud-Native? Get In Touch. Facebook Twitter Youtube Linkedin. What is APM? Subscribe to Stackify's Developer Things Newsletter. The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown.

This is the simplest mechanism for handling exceptions. The try block should be immediately followed either by a catch or finally block. You can also specify specific exceptions you would like to catch. This allows you to have dedicated code to recover from those errors.

This allows you to treat those conditions separately. A try block can be followed by one or more catch blocks, each specifying a different type. The first catch block that handles the exception class or one of its superclasses will be executed. So, make sure to catch the most specific class first. If an exception occurs in the try block, the exception is thrown to the first catch block. If not, the Java exception passes down to the second catch statement.

The name of the NumberFormatException class already tells you the kind of problem. Its message only needs to provide the input string that caused the problem. Most IDEs help you with this best practice. They report an unreachable code block when you try to catch the less specific exception first.

The problem is that only the first catch block that matches the exception gets executed. Always catch the most specific exception class first and add the less specific catch blocks to the end of your list.

You can see an example of such a try-catch statement in the following code snippet. Throwable is the superclass of all exceptions and errors. You can use it in a catch clause, but you should never do it! If you use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors. Errors are thrown by the JVM to indicate serious problems that are not intended to be handled by an application.

Have you ever analyzed a bug report where only the first part of your use case got executed? So, please, never ignore an exception. Someone might remove the validation that prevented the exceptional event without recognizing that this creates a problem.

You should at least write a log message telling everyone that the unthinkable just had happened and that someone needs to check it. That is probably the most often ignored best practice in this list.

You can find lots of code snippets and even libraries in which an exception gets caught, logged and rethrown. It might feel intuitive to log an exception when it occurred and then rethrow it so that the caller can handle it appropriately.

But it will write multiple error messages for the same exception. As explained in best practice 4, the exception message should describe the exceptional event. And the stack trace tells you in which class, method, and line the exception was thrown. Now that you know what exceptions are and how to use them, it's time to learn the advantages of using exceptions in your programs.

Exceptions provide the means to separate the details of what to do when something out of the ordinary happens from the main logic of a program. In traditional programming, error detection, reporting, and handling often lead to confusing spaghetti code. For example, consider the pseudocode method here that reads an entire file into memory. At first glance, this function seems simple enough, but it ignores all the following potential errors.

To handle such cases, the readFile function must have more code to do error detection, reporting, and handling. Here is an example of what the function might look like. There's so much error detection, reporting, and returning here that the original seven lines of code are lost in the clutter. Worse yet, the logical flow of the code has also been lost, thus making it difficult to tell whether the code is doing the right thing: Is the file really being closed if the function fails to allocate enough memory?

It's even more difficult to ensure that the code continues to do the right thing when you modify the method three months after writing it. Exceptions enable you to write the main flow of your code and to deal with the exceptional cases elsewhere.



0コメント

  • 1000 / 1000