Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

Time:2020-9-21

preface

Exception is a kind of instruction flow that causes program interrupt in program. When exception occurs, program will interrupt directly and no further operation will be executed!

Example: divide two numbers. If no exception is handled, the operation result can be displayed only when two numbers are correctly entered.

public static void main(String[] args) {

When the input content is not a number, an exception (numberformatexception) will be generated

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

When all the input contents are numbers, but the divisor is 0, there will be an arithmetical exception (arithmetical operation exception)

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

As a programmer, don’t take it for granted that users will input content strictly according to your requirements. If they make an error in one step of input, the whole program will be interrupted, so be sure to handle the exception! Here is the code after exception handling:

public static void main(String[] args) {

When the input content is not a number, you will be prompted that the input is wrong, and you will be prompted to re-enter:

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

When the divisor is 0, it indicates that the divisor cannot be 0

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

This is called exception handling.

try-catch

If you want to handle the exception, you must use the standard processing format. The syntax of handling format is as follows:

try {

Processing flow:

1. Once an exception is generated, the system will automatically generate an instantiation object of the exception class;

2. At this time, if the exception occurs in the try statement, the matching catch statement will be automatically found for execution. If not, the exception will be thrown;

3. All catches match the instantiated objects of exception classes according to the method parameters. If the match is successful, it means that the catch will be processed,

finally:

After handling the exception, there is a finally statement in the exception handling format. This statement will be used as the unified exit of the exception. No matter whether the exception is generated or not, this code will be executed in the end. Even if no exception occurs and the return statement is used in the try, finally will still execute.

Abnormal architecture

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

Exception refers to exception, exception class. There is a parent class throwable in Java

Throwable has two subclasses:

1. Error: indicates an error and is an error operation issued by the JVM. It can only be avoided as far as possible and cannot be handled by code.

2. Exception: generally, it means that there are errors in all programs, so try will be performed in the program Catch processing.

They are handled in the following two ways:

1

When the program is written, the compiler will automatically check the code, if there is a problem, the code will float red.

For example: sqlexception, IOException, classnotfoundexception, etc.

2

The compiler is unable to detect the code exception, and the program will report an error when it runs.

For example: nullpointexception, arithmethicexception, ClassCastException, arrayindexoutof bundexception, etc.

Attention points of multi exception capture:

1. Catching coarser exception cannot be placed before catching finer exception.

2. For convenience, all exceptions can be caught with exception.

Special multiple exception capture writing method:

Catch (exception type 1 | exception type 2 object name){

The difference between runtimeException and exception:

Integer class: public static int parseInt (string text) throws numberformatexception

This method throws an exception, but it doesn’t need to try Catch capture processing, reason:

Because numberformatexception is not a direct subclass of exception, but a subclass of runtimeException. As long as it is a subclass of runtimeException, it means that the program can operate without using try Catch (no red line), if there is an exception, it is handled by the JVM. Of course, you can also try Catch processing.

Throws keyword

In the program, the basic handling of exceptions has been mastered, but there is also a keyword called throws along with the exception. This keyword is mainly used on the declaration of the method, which means that the exception is not handled in the method, but is handed over to the caller for processing.

Format:

Return value method name () throws exception{

Throw keyword

The throw keyword means that an exception is thrown artificially in a program. From the perspective of exception handling mechanism, once all exceptions are generated, they are actually throwing an instantiated object of an exception class, which can also be thrown directly by throw.

code:

throw new Exception("");

Custom exception class (understanding)

Write a class, inherit exception, and override a parameter construction method, you can complete the user-defined exception type.

Write a class, inherit the runtimeException, and override a parameter constructor to complete the custom runtime exception type.

For example:

Class myexception extends exception {/ / inherits exception and represents a custom exception class

Custom exceptions can do many things, such as:

class MyException extends Exception {

Common interview questions of exception handling

1. Which part of try catch finally can be omitted?

A: either catch or finally can be omitted, but both cannot be omitted at the same time.

Note: the format allows to omit the catch block, but the exception will not be caught when an exception occurs, and the code will not be written in this way during development.

2. In try catch finally, if the catch returns, will finally execute?

A: the code in finally will execute.

Implementation process:

1. Calculate the return value, store the return value and wait for return;

2. Execute finally code block;

3. Return the previously stored return value.

Note:

1. The return value is determined before finally operation and is cached. No matter what changes are made to the value finally, the returned value will not change.

2. It is not recommended to include return in finally code, because the program will exit early in the above process, that is, the returned value is not the value in try or catch.

3. If the JVM is stopped in try or catch, finally will not execute. For example, power failure, or exit through the following code:

JVM:System.exit(0);

3. About the output of the following code:

public class Student {

The correct answer is: the student’s student number is 1000

Solution: first, studentID is 1000, and then run to try to return the student number. At this time, the value 1000 of the basic data type variable studentID will be backed up, and finally the studentID will be changed to 10. However, this only changes the value of the original variable, and the backup value will not change. Finally, the returned value is still the backup value, that is, 1000.

4. Cases similar to the above question:

public class Student {

The correct answer is: the student’s student number is 10

Solution: first, s in the stack memory stores the address of the heap memory. The initial s.studentid in the heap memory is 1000, and then it is executed to try. The address to be returned is the address of the heap memory. At this time, the address will be backed up and finally executed. The s.studentid in the heap memory is changed to 10. Finally, the address of the backup is returned, but the address has never changed The value is the modified 10.

last

Thank you for seeing here. If you think the article is helpful to you, please give me a like. I will share Java related technical articles or industry information every day. You are welcome to pay attention to and forward the article!