Java exceptions which to throw




















Multi-threading in Java. Table of Contents. Save Article. Improve Article. Like Article. Previous Flow control in try catch finally in Java. Next User-defined Custom Exception in Java. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. Just good old functions returning values.

On the other hand, this error-handling strategy encourages the consumer of a function to always check for error values.

The sheer number of those verifications can make the code polluted. Lots of defensive checks might hide important business logic, making the code less readable and maintainable.

Another serious disavantage of error codes is their lack of context. Something went wrong while initializing an object? When an exception is thrown, the control flow of the program is interrupted. If no one handles the exception, it causes the program to crash. The message might not even be localized to their language. To handle the exception that was thrown, we have to catch it. We do this by using an exception-handling block.

There, we can make the necessary arrangement in order to deal with the exception. Still sounds too abstract? Keep reading. After printing the third line, the code initializes an array with three integers, and pass it as an argument to a private method. The fourth and fifth messages are never displayed. Neither is executed the second line in of the print4thItemInArray method. This time, the exception still happens, same as before. The flow control of the program is then handed to the catch block.

Then, execution is resumed. Fortunately, for you, we have another post in which we cover at length the details of how to handle exceptions. Go check that out if you need to learn more! And without further ado, here are the list of best practices we promised you.

It happens quite often that you use a resource in your try block, like an InputStream , which you need to close afterward. A common mistake in these situations is to close the resource at the end of the try block.

The problem is that this approach seems to work perfectly fine as long as no exception gets thrown. All statements within the try block will get executed, and the resource gets closed. You call one or more methods which might throw an exception, or maybe you throw the exception yourself. That means you might not reach the end of the try block. Throwing an exception is as simple as using the "throw" statement.

You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc. Here is an example that shows how to throw an exception:. Throws is a keyword used to indicate that this method could throw this type of exception. The caller has to handle the exception using a try-catch block or propagate the exception.

We can throw either checked or unchecked exceptions. The throws keyword allows the compiler to help you write code that handles this type of error, but it does not prevent the abnormal termination of the program. With the help of the throws keyword, we can provide information to the caller of the method about the types of exceptions the method might throw. For example:. In the example below, we have created a test method to demonstrate throwing an exception.

Download your free, two week trial today. 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. Figure 1: Types of Exceptions in Java. About the Author Latest Posts. His writing coverage includes companies such as iSkysoft, Keepvid, Xpo2, Stackify, Entrepreneurbus, etc. Get In Touch. Facebook Twitter Youtube Linkedin. What is APM? Subscribe to Stackify's Developer Things Newsletter. Sign Up Today. Start Your Free Trial Now.



0コメント

  • 1000 / 1000