Java Exception Handling

Source: Internet
Author: User
Tags finally block

• Exceptions: In the Java language, abnormal conditions that occur in program execution are referred to as "exceptions". (Syntax errors and logic errors during development are not exceptions) · The exception events that occur during the execution of a Java program can be divided into two categories: a serious problem that error:  a Java virtual machine cannot resolve. such as: JVM system internal error, resource exhaustion and other serious situations.    Typically do not write targeted code for processing. Exception: Other general issues caused by programming errors or accidental external factors can be handled using targeted code. For example: null pointer access attempts to read nonexistent file network connection interrupts • For these errors, there are generally two workarounds: one is to terminate the program's operation if an error is encountered. Another method is that the programmer, when writing a program, takes into account the error detection, the hint of the error message, and the handling of the error. • Capturing errors most ideally during compilation, but some errors occur only at run time. For example: Divisor is 0, array subscript out of bounds and other categories: compile-time exceptions and run-time exceptions 1. Runtime Exceptions • Refers to exceptions that the compiler does not require for forced disposition. Usually refers to the programming logic error, is the programmer should actively avoid its occurrence of the exception. The Java.lang.RuntimeException class and its subclasses are run-time exceptions. • This type of exception can be handled without processing because such exceptions are common, and if full processing may have an impact on the readability and operational efficiency of the program 2. Compile-time exceptions • Refers to exceptions that the compiler requires to be disposed of. This is the general exception caused by external factors when the program is running. The compiler requires that a Java program must capture or declare all compile-time exceptions. • For such exceptions, if the program does not process, it may bring unexpected results.  java exception handling Java using exception handling mechanism, the exception handling program code together, and normal program code separate, make the program concise, and easy to maintain. Java provides a catch-and-throw model of exception handling. When an exception occurs during the execution of a Java program, an exception class object is generated, and the exception object is presented to Java, which is called a throw (throw) exception. • The generation of exception objects is automatically generated by the virtual machine: The virtual machine detects a problem when the program is running, If the corresponding handler is not found in the current code, an instance object corresponding to the exception class is automatically created in the background and thrown--automatically thrown by the developer manually: Exception Exception = new ClassCastException ();-- Creating a good exception object does not throw any effect on the program, and creates a normal object as   If an exception is thrown inside a method, the exception object willis thrown into the caller's method for processing. If the exception is not handled in the caller's method, it continues to be thrown to the upper method of the calling method. This process will continue until the exception is handled. This process is called a catch (catch) exception. • If an exception is returned to the main () method and main () is not processed, the program runs to terminate. • Programmers can usually only handle Exception, and there is nothing to do with error. The first step in try catching an exception is to use the try{... The statement block selects the scope of the catch exception and puts the code in the TRY statement block that may be the exception. catch (Exceptiontype e) is the code that handles the exception object in a Catch statement block. Each try statement block can accompany one or more catch statements to handle the different types of exception objects that may be produced. • Information about catching exceptions: As with other objects, you can access the member variable of an exception object or the method that calls it GetMessage () Gets the exception information, returns the string p Rintstacktrace () Gets the exception class name and exception information, and where the exception appears in the program. return value void.  Finally, the last step of catching an exception is to provide a unified exit for exception handling through the finally statement, allowing control to flow to other parts of the program before it can manage the state of the program in a unified way.  Whether an exception occurs in a try block, the catch statement is executed, the Catch statement has an exception, and the statement in the Catch statement is executed in the return,finally block. The finally statement and the catch statement are optional when the exception is not caught• The exception used earlier is the RuntimeException class or its subclasses, and the exceptions to these classes are characterized by:

Even if the try and catch captures are not used, Java can capture it itself and compile it (but the run-time exception causes the program to stop running).

• If the exception thrown is a non-runtime exception of type IOException, then it must be caught, otherwise a compilation error occurs. That is, we have to handle compile-time exceptions, catch exceptions, and convert them to run-time exceptions.

Declaration throws an exception

• Declaring throw exceptions is the second way to handle exceptions in Java

If a method (in which the statement is executed) may generate an exception, but is not sure how to handle the exception, this method should display a declaration that throws an exception, indicating that the method will not process the exception and is handled by the caller of the method.

A throws statement can be used in a method declaration to declare a list of thrown exceptions, which can be either the type of exception produced in the method or the parent class of the throws.

Overriding method declares the principle of throwing exceptions

• The override method cannot throw an exception type that is larger than the overridden method's scope.

Manually throwing Exceptions

· Java Exception class objects are automatically generated and thrown by the system except when an exception occurs during program execution, and can be created and thrown manually as needed.

Create a user-defined exception class

A user-defined exception class that describes the data value azimuth error information. The user's own exception class must inherit the existing exception class.

Java Exception Handling

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.