JAVA basics exception Overview

Source: Internet
Author: User

Summary: 1. Exceptions give beginners a feeling (I felt like this at the time, but I don't know if other people have a resonance ?) 1. What is an exception? Cannot understand 2. very complicated 3. the exception is very simple. It is useless. I guarantee that the correctness and robustness of the program I wrote won't be used. 2. explanation: 1. exceptions are a mechanism that java uses to handle program errors. The so-called errors are not syntax errors, but unexpected runtime errors (Major. as long as you use it, exceptions are not complicated (for example, generic threads. on the contrary, exceptions are very powerful, provided that you have to use 4. remember that you are a human, not a god, and you cannot enumerate all the errors. Even if you have the ability to do so, your program will be quite bloated (write 1000 lines of programs, row 3 processes all possible situations, whether important or not ). 3. Significance of the existence and use of exceptions 1. Make up for our lack of human privacy, handle unpredictable errors, and ensure that the program runs properly. 2. Simplified development. You do not need to enumerate all possible situations. You only need to do useful information related to the business logic. Other unimportant (relative to the business logic) will help you deal with exceptions. 4. in java, exceptions and errors are divided into two types: Exception (Exception) and Error (Error). These two classes are the Exception classes of Throwable: we mainly discuss the Errors generated during program running. Error: a VM-level Error. Generally, we cannot handle it from the perspective of a program. 5. the following is an exception [java] package com. cxy. exception; import java. util. list;/** demo of several common exceptions (exception overview) * @ author cxy */public class SomeException {public static void main (String [] args) {try {test (21,0);} catch (Exception e) {e. printStackTrace ();} // The access index is 5 elements, an array out-of-bounds exception will be thrown. The following ArrayIndexOutOfBoundsException try {// actually does not need to be set here, but for the sake of clear structure, I put this line at this position String [] array = {"", "B", "C", "D", "E"}; System. out. println (Array [5]);} catch (Exception e) {e. printStackTrace ();} try {// The following sentence is not necessary here, but for the sake of clear structure, I put this line to this position List l = null; // beginners are used to initializing a List l. get (0); // try to access a null object, it will throw a null pointer (reference) exception NullPointerException // Of course you will say that I will not be so stupid to access a null method, this example is just a simple epitome. The real program may not be expected to be null} catch (Exception e) {e. printStackTrace ();} System. out. println ("normal program running");}/*** when B is 0, an arithmetic error exception is thrown. ArithmeticException */www.2cto.com Public static void test (int a, int B) {System. out. println ("a/B =" + a/B);} Conclusion: 1. the above program demonstrates three common exceptions, so that you can understand exceptions from the senses. although the program has three exceptions, it is found that the program is still running normally. This is one of the values of exceptions (ensure that the program runs normally. the console prints information about various exceptions. When developing, Beginners should pay attention to these exceptions, which is also one of the ways to improve development efficiency.

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.