A simple understanding of Java exceptions and a simple understanding of Java

Source: Internet
Author: User
Tags finally block

A simple understanding of Java exceptions and a simple understanding of Java
What is the essence of Java exceptions?

Essentially, a java exception is a java Object (inheriting objects). Like a general java Object, it encapsulates some member variables and operations, we can operate java exception objects as we operate on general java objects (I also tried it specially, but can I use the throw keyword to operate General java objects and find compilation errors, prove that it is only the syntax of the Operation exception class)

In terms of meaning, java exceptions are errors that are not expected by the program. However, due to factors such as the design logic and running environment of the program, exceptions are inevitable. the Java Virtual Machine defines many exception classes for different exceptions. When the jvm running program discovers the corresponding exceptions, it will throw an exception to the outside.

How does java handle exceptions?

Java provides the logic for throwing exception capture exceptions to handle exceptions. When a program throws an exception, if the exception capture logic does not exist, the method being executed will stop the execution and the exception will be thrown out, the program that calls this method performs the same processing. If no capture is performed, the program throws an outward layer by layer until the thread reaches the current thread.

The logic for exception handling is to use try catch finally statement block, which allows Exception Handling for captured exceptions. Similar to a condition judgment, what should we do when an exception occurs, for details about the syntax rules of try catch finally statement blocks, refer to other documents. Here we will talk about some areas that are easy to ignore:

1) There are operations on the same variable in the try block and finally block, and the variable will be returned in the try block, as shown below:

public static int test(){        int i=10;        try{            return i;        }finally{            i= i+2;        }    }

Then, what value will be returned when this method is called? In principle, finally will be executed and 12 should be returned. In fact, 10 will be returned, because a temporary variable will be created when the return statement is executed to store the return value I, after finally is executed, the value of the temporary variable is returned. For the basic data type, because it is a value transfer, all finally operations are the variables in the method, but the data of the temporary variables is not operated, so it does not take effect, if it is an object, because the temporary variables store the object variables, the next operation in finally will take effect. Here, some problems are that the packaging class and the String class will not take effect like the basic type, I don't know why.

2) if it is a custom java type, the finally modification will take effect as follows:

public static data test(){        data d = new data();        try{            return d;        }finally{            d.inte=15;        }    }    private static class data{        public int inte=10;    }
Java exception Classification

All java Exception classes are extended to the Throwable class, And the Throwable class has two subclasses: Error and Exception. The Error class indicates errors caused by the virtual machine, for example, internal virtual machine errors and memory overflow. Exceptions are errors caused by the running status of the program, including program logic problems and program IO interaction problems. In programming, there is almost no concern about Error. In other words, an Error Exception is basically powerless except restart. This is a problem of the jvm itself, and we are more concerned about Exception errors.

The Exception class contains two types of errors:

1) RuntimeException (runtime exception). This is a problem in the logic of program design. To put it bluntly, it is your problem. Excellent programmers usually seldom make such errors, it is hard for a newbie to explain. The following lists common RuntimeException: do not capture this exception to solve the problem. You should avoid this exception from the perspective of the design program, for example, when calling a method that may encounter the following exceptions, it is best to do a check before calling the method.

      • NullPointerException: NULL pointer exception, which is caused by the definition of a variable called before it is instantiated.
      • ClassCastException: type forced conversion exception
      • IllegalArgumentException: Invalid parameter exception
      • ArithmeticException: arithmetic running exception
      • ArrayStoreException: An error occurred while adding an invalid object type to the array.
      • ArrayIndexOutOfBoundsException: array subscript out-of-bounds exception (this exception is common)
      • NegativeArraySizeException: this error will be reported when an array is created using a negative number to determine the array size.
      • NumberFormatException: number formatting exception. This error is usually reported when the number of the string is converted to the int type. It may be that the string cannot be converted at all (not a number or number that has been spliced with spaces)

2) IOException (IO exception): This type of exception is usually caused by a program dealing with resources or interfaces. The program itself cannot control the correctness of resources and interfaces, therefore, this type of exception is allowed in the program. We usually need to capture this type of exception to determine what should be done in the case of an exception. The common IOException is as follows:

      • ClassNotFoundExcpetion
      • NoClassDefException: Class dependency exception. A common exception in spring's ioc framework must be that a dependency class is not defined (this occurs when maven is used for management exceptions, the relevant jar package has been imported, but this exception still occurs. You can delete the corresponding package of the dependency exception in the local repository and reload it)
      • FileNotFoundException: the file cannot be found. An exception occurred.

Off-the-question: If java is able to handle exceptions in a timely manner, it will not be allowed to appear if it is controlled by a program. If it cannot be controlled by a program, it will be captured for processing, in addition, we should remember some basic exceptions. If you say you have never seen any exceptions, the interviewer will ask you about some of your common exceptions.

 

Related Article

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.