Those years, we do not understand but have to mention the Java exception and exception handling!

Source: Internet
Author: User
Tags finally block

---restore content starts---

First of all, I am a small rookie, recently suddenly whim, want to study the Java exception and exception handling, a little bit of understanding, the birds do not want to ridicule ...

Since we want to deal with exceptions and exceptions, we need to understand the anomalies first, so what is an exception?

Simply put, I call it an exception when a program is running and an unknown error occurs while it stops running.

It can be said that the exception is always around us, from the simple Code link, to life, there may be anomalies. For a percentage, when we go hiking,

To sit the cable, but unfortunately, the cable is dead, how to do? In general we have two solutions, one is to climb up and one is to stay where you are.

Now in the analysis from this example:

People ride cables to the mountains, which can be seen as code at runtime.

The cable is out of power, people stay in place, this can be regarded as an anomaly.

The cable is out of power, we climb up, this can be said to be abnormal handling.

So much nonsense, just want to let the younger than me more easily understand the anomaly, then we directly to use the code to see what is abnormal.

Code with Error

Error messages that are displayed after running

This section of code will run with an error, and compile without error, this is the exception! (Comparative Professional, no life small case list easy to understand >*-*<)

The above is an exception, but how is the exception displayed in the console? Next I'm going to introduce you to the unusual architecture and branching!

Where Throwable is the superclass of all exceptions, he inherits with the object class. Because all classes inherit the object class

and Throwable downstream two branches of error and exception class,

Where error means only critical errors that cannot be recovered by the program itself, such as memory overflow dynamic link failure, virtual machine error.

Exception is a non-critical error thrown and handled by a Java application.

And the exceptions we often use are the following

Exception: Top-level exception;

ArithmeticException: Arithmetic error situations, such as dividing by 0.

ArrayIndexOutOfBoundsException: Array subscript is out of bounds.

NullPointerException: Attempt to access a null object member.

ClassNotFoundException: The required class cannot be loaded.

Inputmismatchexception: The type of data to be obtained does not match the type of the actual input.

IllegalArgumentException: The method received an illegal parameter.

ClassCastException: Object coercion type conversion error.

NumberFormatException: Numeric format conversion exception, such as converting "ABC" to a number.

The introduction of the exception to the first here, since we know the anomaly, we should know how to catch the anomaly,

When we catch an exception, we need to use the try-catch-finally statement.

Let's talk about his grammar:

try{

code block

}catch (Exception e) {

code block

}finally{

code block

}

His execution is to execute the contents of the try block first, and when the content in the try is wrong, walk the catch block, but the finally block will always be executed.

Unless you don't write.

After his execution, we're talking about the combination of the three modules.

1.2.

try{try{

}catch{ }finally{

}           }

To the last side of the time to give you an unusual capture of the face question.

  

The exception catch is here first, let's say the exception of the two keywords, throws and throw.

Throw is an exception, and throws is a declaration exception,

  

When you throw an exception, if you do not use the Try-catch statement for exception capture, you must declare it, indicating who will call me this method who is going to do an exception capture.

The next question came out:

A person with a heart can think about the results of this question and see how many are returning, using the brain. The answer is at the bottom!

The answer is 2, because the value of return I in the try block is secretly opened up in virtual memory by the virtual machine, and this space stores the value of the first return.

The answer to the correct and not correct remember the message oh ... In fact, I am a small rookie!

}

Those years, we do not understand but have to mention the Java exception and 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.