Code execution flow When an exception occurs in Java

Source: Internet
Author: User

Exceptions and Errors:
Abnormal:
In Java, the error of the program is mainly a syntax error and semantic error, a program in the compilation and operation of the error we call an exception, it is a VM (virtual machine) notify you a way, in this way, the VM let you know that you (the developer) has made a mistake, there is now a chance to modify it. Exception classes are used in Java to represent exceptions, and different exception classes represent different exceptions. But all exceptions in Java have a base class, called exception.
Error:
It refers to a serious problem that a reasonable application cannot intercept. Most of them are abnormal situations. The error is a failure of the VM (although it can be any system-level service). So, errors are difficult to handle, and the average developer (not you, of course) cannot handle these errors, such as memory overflows. As with exceptions, error classes are used in Java to represent errors, and different error classes represent different errors. But all the errors in Java have a base class called error.
In summary, we can know that the most essential difference between anomalies and errors is that the exception can be handled by the developer and the error when the system was originally brought, and generally can not be processed or need our programmers to deal with.
1. An exception is an event that occurs during the execution of a program that interrupts the operation of the normal instruction
2. Error, an action or instance that deviates from acceptable code behavior
Structural Classification of exceptions:
1. Run-time exception (exception not checked)
2, compile-time exception (checked exception)
Running an exception is runtimeexception; all the rest is a compilation exception
There is a common parent class throwable in Java for exception exception and error errors.
Error Exception
RuntimeException several sub-classes
1, Java.lang.ArrayIndexOutOfBoundsException
Array index out-of-bounds exception. Thrown when the index value of an array is negative or greater than or equal to the size of the arrays.
2, Java.lang.ArithmeticException
Arithmetic condition exception. For example: integers except 0.
3, Java.lang.NullPointerException
Null pointer exception. This exception is thrown when an app tries to use null where the object is required. For example: Invoking an instance method of a null object, accessing a null object
property, evaluates the length of the null object, throws null using the throw statement, and so on
4, Java.lang.ClassNotFoundException
The class exception could not be found. When an application attempts to construct a class based on the class name in the form of a string, and cannot find a class file with the corresponding name after traversing Classpah, it throws
The exception.
Handling of Exceptions:
try{}catch{}
try{}catch{}finally{} Whether or not an exception is found in the finally code block will be executed
try{}finally{} can also be used in combination, but catch{}finally{} cannot be
Note: In an inheritance relationship, a subclass overrides a method of the parent class, and the scope of the thrown exception cannot be broader than the parent class
Use of exceptions
In the exception of the use of this part of the main demo code, are we usually write code in the process will encounter (of course, only a small part), to stimulate it!

The following example illustrates the execution flow of code after an exception by comparing two methods.

 Public classException1 { Public Static voidTestException1 () {int[] INTs =New int[] {1, 2, 3, 4 }; System.out.println ("Before the exception occurs"); Try{System.out.println (ints[4]); System.out.println ("Did I have the honour to do so?");//After an exception occurs, the following code cannot be executed}Catch(indexoutofboundsexception e) {System.out.println ("Array out of bounds error"); } System.out.println ("After the exception has occurred"); }     Public Static voidMain (string[] args) {testException1 (); }}

Operation Result:

Before an exception occurs
Array out of bounds error
After an exception occurs

 Public classException2 { Public Static voidTestException2 () {int[] INTs =New int[] {1, 2, 3, 4 }; System.out.println ("Before the exception occurs"); System.out.println (ints[4]); System.out.println ("Did I have the honour to do so?");//After an exception occurs, the code behind him cannot be executed        }     Public Static voidMain (string[] args) {testException2 (); }}

Operation Result:

Exception occurs before exception in thread "main"
Java.lang.arrayindexoutofboundsexception:4
At Exception2.testexception2 (exception2.java:6)
At Exception2.main (exception2.java:11)

Summary: First point out the shortcomings of the example, Indexoutofboundsexception is a non-inspected exception, so do not try...catch ... Show snapping, but my goal is to treat the same exception in a different way, to see what it will look like (it's going to have to be done here). When an exception occurs, the first method simply jumps out of the try block, but the code behind it executes as well. But the second one is not the same, just jump out of the way, more tough. From the first method we see that try...catch ... is a "transactional" guarantee, which is designed to ensure that the program runs in an abnormal situation, and that it tells the programmer the details of the error in the program (this detail is sometimes dependent on the programmer's design).

Code execution flow When an exception occurs in Java

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.