The advantages and disadvantages of exception handling in Java programming

Source: Internet
Author: User
Tags exception handling try catch

Exception handling in Java programming is a common topic, and almost any introductory Java course will mention exception handling. However, I think a lot of people actually do not really master the correct way to deal with abnormal situations and strategies, at most, but also know the approximate point of view. In this paper, we discuss three kinds of Java exception handling with different degrees and quality, and the way of dealing with abnormal methods is divided into:

Good, bad and bad three kinds.

It also gives you some tips on how to solve these problems.

First, explain some definitions and mechanisms that must be clarified in Java exception handling. Any violation that derives from the error class or the RuntimeException class is referred to by the Java language specification as an "Unchecked" exception, and all other exceptions are called "verifiable" (Checked) exceptions.

The so-called inspection anomaly refers to the exception that we should handle ourselves. As for the means of handling, either control (try catch) or notice (throws) they are likely to produce. In general, you should catch those exceptions that you know how to handle, and advertise those exceptions that you do not understand how to handle.

And for those that cannot be checked, they are either outside our control (Error) or we should not allow it first (runtimeexception).

As for the exception designation, the Java rule is very simple: a method must notify itself of all possible check exceptions that may be generated. When writing your own method, it is not necessary to notice every exception object that the method is actually likely to produce, and to understand when the throws of the method must be communicated to the exception, one must know that for an exception he is likely to produce only four of the following:

1. A method that could produce an exception was invoked. For example, the ReadLine method of BufferedReader class. This method informs Java.io.IOException exceptions

2. An error was detected and an exception was generated with the throw statement.

3. A programming error has occurred. For example A[-1] = 0.

4. Java generates an internal error.

If you have one of the first two cases, you must tell the person who intends to use your method: If you use this method, you may create an exception (that is, use throws on the method head), a simple memory method:

As long as contains throw, it is necessary to notify throws. If a method must handle multiple exceptions at the same time, all exceptions must be indicated within the header. As shown in the following example, divide them by commas:

class Animation
{
   public Image loadImage(Strint s) throws EOFException,MalformedURLException
   {
    …………
   }
}

However, we do not need to advertise internal Java errors and should not advertise exceptions derived from RuntimeException.

Good exception handling

Good exception handling provides a uniform mechanism for handling program errors. In fact, the Java language significantly improves the ability of exception handling in software development by presenting an exception warning to callers. This approach extends and enhances the method in the Java language to include its own error conditions. Let's take a look at an example that illustrates this situation.

The following is a prototype of one of the FileInputStream constructors:

Public FileInputStream (String name) throws FileNotFoundException Java

Methods and constructors must declare that they may "throw out" the exception when they are invoked, using the keyword "throws". This exception hint in the method prototype increases the reliability of the program.

Obviously, this approach prompts the caller of the method for possible exception conditions so that the caller can appropriately handle the exceptions. The following code signals how we caught and handled the exception of FileNotFoundException:

try 
{
   FileInputStream fis = new FileInputStream(args[0]);
   // other code here ...
}
catch (FileNotFoundException fnfe)
{
   System.out.println("File: " + args[0] + " not found. Aborting.");
   System.exit(1);
}

Java exception handling has other excellent features, such as checking for exceptions, user-defined exceptions, and the new Java logging APIs (Java Logging APIs) that are introduced in JDK 1.4. All subclasses of java.lang.Exception belong to a check exception. The check exception (checked exception) is the exception that must be prompted by the method that throws the exception, which must be caught or prompted to the caller. User-defined exceptions (user-defined exceptions) are custom exception classes that extend the Java.lang.Exception class. Excellent Java programs stipulate custom exception encapsulation, reporting, and handling of their own unique situations. The latest Java Record API (Logging API) allows you to centralize the recording of exceptions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.