Dark Horse programmer-exception in JAVA learning logs

Source: Internet
Author: User

Dark Horse programmer-exception in JAVA learning logs

Log learning exception

Exception (overview and classification of exceptions)

A: exception Overview

An exception is an error that occurs when the Java program is running. Exceptions refer to unexpected situations, such as file failure, network connection failure, and invalid parameters. An exception is an event that interferes with the normal command flow during the running of the program.

B: exception Classification

Java describes various exceptions through the many subclasses of the Throwable class in the API. Therefore, Java exceptions are all objects and are examples of the Throwable subclass.

View Throwable through API

Error

Is an error that cannot be processed by the program, indicating a serious problem in running the application. Most errors are irrelevant to the operations performed by the code writer. They indicate problems with JVM (Java Virtual Machine) during code execution, server downtime, database crashes, and so on.

Exception

Is an exception that the program itself can handle.

Note: The difference between exceptions and errors: exceptions can be handled by the program itself, but errors cannot be handled.

C: Abnormal Inheritance System

Throwable

Error

Exception

RuntimeException

Note: The difference between exceptions and errors: exceptions can be handled by the program itself, but errors cannot be handled.

 

 

How does JVM handle exceptions by default?

The main function can handle this problem in two ways:

A: handle the problem and continue running it.

B: There is no specific processing method. It can only be handled by calling the main jvm.

Jvm has a default Exception Handling Mechanism to process the exception.

And print the Exception name and information. The exception location is on the console, and the program stops running.

Exception (try... catch Method to handle exception 1)

A: Two Methods for Exception Handling

A: try... Catch... Finally

Trycatch

Trycatch finally

Tryfinally

B: throws

Try: used to detect exceptions

Catch: used to catch exceptions

Finally: used to execute mandatory statements. For example, release resources.

B: try... catch: the basic format for Exception Handling

Try... Catch... Finally

Differences between Android and J2EE in handling exceptions:

Android: because it is a client, the exception is usually handled privately to ensure that the APP can continue to run. But in this way, exceptions will be buried and hidden risks will be left behind.

J2EE: because it is a server-side development, it is generally the underlying development, so exceptions cannot be handled without authorization, leaving hidden risks. Instead, the problem is thrown up to generate a log file.

C: Case Study

Try... catch Method to handle 1 exception

 

 
        try {                   int x = 10,y = 0;                     int c = x / y;         }catch (Exception e) {                   System.out.priintln(e);              }

Exception (try... catch Method to handle exception 2)

A: Case Study

Try... catch to handle multiple exceptions

Try {Statement 1; // Exception A occurs. The exception is caught when the program runs here, therefore, neither the following statement 2, Statement 3, nor the catch statements that capture them will execute // line.
Statement 2; // exception B Statement 3; // exception C} catch (ExceptionA) {System. out. println ("Exception A");} catch (ExceptionB) {System. out. println ("exception B");} catch (ExceptionC) {System. out. println ("abnormal C ");}

Exception A occurs. During the actual operation, the program runs here and the exception is caught. Therefore, the following statement 2 and the catch statements that capture them will not be executed.

So catch after try, with small exceptions placed in front and large exceptions placed in the back. According to the principle of polymorphism, a large exception will accept the objects of the left and right sub-classes, so that the exception is eaten, so you cannot know what the exception is.

Methods and precautions for handling multiple exceptions after JDK 7

 

Try {Statement 1; // Exception A Statement 2; // exception B} catch (ExceptionA | ExceptionB ){}

 

 

 

Exception (the difference between an exception during compilation and an exception during runtime)

Generally, Java exceptions (including exceptions and errors) are dividedChecked exceptions and unchecked exceptions).

 

Queryexception (exceptions that must be handled by the compiler):

When the right program is running, it is prone to reasonable and reasonable exceptions. Although an exception can be queried, it can be predicted to some extent. In addition, once such an exception occurs, some methods must be taken to handle it.

Except for RuntimeException and its subclasses, all other Exception classes and their subclasses are queryable exceptions. This exception is characteristic of the Java compiler checking it. That is to say, when such exceptions may occur in the program, either catch it with a try-catch statement or throws it with a throws clause declaration, otherwise, compilation fails.

Non-checkable exceptions (exceptions that are not forcibly handled by the compiler): including runtime exceptions (RuntimeException and its subclass) and errors (errors ).

Uncheckable exceptions are not found during compilation, that is, they do not need to be thrown or processed during code writing. When a program is executed, the program stops directly.

The queryable exception must be handled or thrown when writing code. If it is not processed or thrown, a red alarm is triggered.

Exceptions are classified into two types: runtime exceptions and non-runtime exceptions (compilation exceptions ). The program should try to handle these exceptions.

Runtime exception:All are RuntimeException classes and their subclass exceptions, such as NullPointerException (NULL pointer exception) and IndexOutOfBoundsException (subscript out-of-bounds exception). These exceptions are non-check exceptions and can be captured and processed in programs, or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions logically.

The runtime exception is characteristic that the Java compiler does not check it. That is to say, when such exceptions may occur in the program, even if the try-catch statement is not used to catch it, it is not thrown by the throws clause declaration, it will also be compiled.
Non-runtime exception (compilation exception ):Is an Exception other than RuntimeException, and belongs to the Exception class and its subclass. From the perspective of program syntax, it is an exception that must be processed. If it is not processed, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception. Generally, no custom check Exception occurs.

 

Exception (several common methods of Throwable)

A: several common methods of Throwable

A: getMessage ()

Obtains the exception information and returns a string.

B: toString ()

Obtains the exception class name and exception information, and returns a string.

C: printStackTrace ()

Obtain the exception class name and exception information, and the location where the exception occurs in the program. Return Value void.

B: Case Study

Basic usage of several common Throwable Methods

 

 

Exception (throws handle exceptions)

A: throws Exception Handling

When defining functional methods, you need to expose the problems and let the caller handle them.

Then it is identified by throws on the method.

Throws throw an exception rule:

1) if the exception is unchecked (unchecked exception), that is, Error, RuntimeException, or their subclasses, you can declare the exception to be thrown without using the throws keyword, and the compilation can still pass smoothly, but it will be thrown by the system at runtime.

2) You must declare any checked exception thrown by the method ). That is, if a method may encounter a queryable exception, either capture it with a try-catch statement or throw it with a throws Clause Declaration. Otherwise, compilation errors may occur.

3) only when an exception is thrown, the caller of the method must handle the exception or throw the exception again. When the caller of the method is unable to handle this exception, it should continue to throw instead of waiting for the exception.

B: Case Study

Void method () throws javastiona {

If (true)

Throws new partition tiona ();

}

Exception (throw overview and difference with throws)

A: throw Overview

In some internal situations of function methods, the program cannot continue to run. When a jump is required, throw the exception object.

B: Case Study

Demonstrate the throwing of exception objects during compilation and exception objects during runtime respectively

C: difference between throws and throw

A: throws

Used after the method declaration, with the exception class name

Can be separated with multiple exception class names by commas

Indicates that an exception is thrown and is handled by the caller of this method.

PS: it is a statement that tells others that they may have exceptions. Pay attention to it.

B: throw

Used in the method body with the exception Object Name

Only one exception object name can be thrown

Throws an exception and is processed by the statement in the method body.

 

Exception (features and functions of finally keywords)

A: finally features

The statement body controlled by finally must be executed.

Special case: the jvm exits before it is executed to finally (for example, System. exit (0 ))

B: Role of finally

This API is used to release resources and will be seen in IO stream operations and database operations.

C: Case Study

Features and functions of finally keywords

Exception (finally keyword interview questions)

A: interview question 1

Differences between final, finally and finalize

B: interview question 2

If there is a return statement in catch, will finally code be executed? If yes, whether it is before or after return.

When a return statement is encountered in a try block or catch block, the finally statement block will be executed before the method returns.

Exception (custom exception overview and basic usage)

A: Why do I need custom exceptions?

To further refine the exception details.

B: Overview of custom exceptions

Inherited from Exception

Inherited from RuntimeException

Exception (Precautions for exceptions and how to handle exceptions)

A: Exceptions

A: When a subclass overrides the parent class method, the subclass method must throw the same exception or a subclass of the parent class exception. (The father is broken. The son cannot be worse than the father)

B: If the parent class throws multiple exceptions, when the child class overrides the parent class, it can only throw the same exception or its subset. The Child class cannot throw exceptions that the parent class does not have.

C: If the overwritten method does not throw an exception, the subclass method cannot throw an exception. If an exception occurs in the subclass method, the subclass can only try, not throws.

B: How to handle exceptions

Principle: If this function can internally handle the problem, use try. If it cannot be processed, it will be handled by the caller. This is throws.

Differences:

To continue running the subsequent programs, try

Throws when subsequent programs do not need to continue running

If the JDK does not provide the corresponding exception, you need to customize the exception.

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.