Java journey (1) --- about "exceptions" and java journey

Source: Internet
Author: User
Tags integer division

Java journey (1) --- about "exceptions" and java journey
I have heard of "exceptions" since I started learning VB. I think that exception handling is a try .. catch, do not let the error be thrown to the user, so far, do not ask again, no more. This is what we understood at that time. At any time, the increase in project experience and the basic learning of Java have some different feelings about "Exception Handling". I would like to share it with you.


JavaException: Basic knowledge


(1)Exceptions and errors

In Java, program errors are mainly syntax errors and semantic errors. errors that occur during compilation and runtime of a program are collectively referred to as exceptions. They are VM (Virtual Machine) errors) one way to notify you is to give you a chance to modify it. In Java, exception classes are used to indicate exceptions. Different exception classes represent different exceptions. However, all exceptions in Java have a base class called Exception.


The Error corresponding to the exception isIt refers to a serious problem that a reasonable application cannot intercept. Most of them are abnormal. An error is a fault of the VM (although it can be a service of any system level), such as memory overflow. Similar to exceptions, errors are represented by error classes in Java. Different error classes indicate different errors. However, all errors in Java have a base class called Error.


(2) Abnormal Structure Classification

It mainly includesRuntime exceptionAndException during compilation. RuntimeException is a non-checked error, which is a major difficulty for programmers to handle. compile-time exceptions are checked errors.

As mentioned above, different exception classes in Java represent different exceptions. Exceptions during running mainly include the following sub-classes:


1. java. lang. ArrayIndexOutOfBoundsException
An error occurred while exiting the array index. If the index value of the array is negative or greater than or equal to the size of the array, throw.


2. java. lang. ArithmeticException
Arithmetic condition exception. For example, integer division by zero.


3. java. lang. NullPointerException
Null Pointer exception. This exception is thrown when the application tries to use null where the object is required. For example, call the instance method of the null object, access the attribute of the null object, calculate the length of the null object, and throw null using the throw statement.


4. java. lang. ClassNotFoundException
Class exception not found. This exception is thrown when the application tries to construct a class based on the class name in the string format and cannot find the class file with the corresponding name after traversing the CLASSPAH.


RuntimeException and Error can be generated in any code. They do not need to be thrown by the programmer. Once an Error occurs, the corresponding exception is automatically thrown. The check exception is thrown by the programmer. There are two situations: the client programmer calls the library function that throws an exception (the Library Function exception is thrown by the library programmer ); the client programmer throws an exception using the throw statement.

When an Error occurs, the programmer is generally powerless. When a RuntimeException occurs, the program must have a logic Error and must be modified (equivalent to a debugging method ), understanding the four main types of RuntimeException categories above makes it more targeted to debug and modify our logic errors. Only checked exceptions are of concern to programmers, the program should and should only throw or handle checked exceptions.


JavaException: Handling Method


After learning about Java exceptions, you need to know how to handle them. The main process of Java exception handling is as follows:

① If an error occurs, the method ends immediately without returning a value. At the same time, an exception object is thrown. ② The program that calls this method will not continue to execute, but will search for an exception processor that can handle the exception and execute the code.
There are two processing methods:

(1) Capture

For capturing methods, Java provides targeted processing methods, that is, try... Catch:

Try {// code to be detected;} catch (exception class variable name) {// Exception Handling Code;} fianlly {// the code to be executed; it mainly allows users to close resources. Whether or not an exception occurs, the resource must be closed .}

In programs, we usually use the following methods to display exception information:

Catch (Exception e) {// e is used to receive Exception objects detected by try. System. out. println ("message:" + e. getMessage (); // obtain the exception information. System. out. println ("toString:" + e. toString (); // obtain the Exception name + exception information. E. printStackTrace (); // print the information of the exception in the stack; Exception name + exception information + exception location .}

(2) Throw

1. Method for throwing an exception

In the method declaration, add the throws clause to indicate that the method will throw an exception. The Declaration format of methods with throws clauses is as follows:

[<Modifier>] <return value type> <Method Name> ([<parameter list>]) [throws <exception class>]

Throws is the keyword and <exception class> is the exception class to be thrown by methods. Multiple exception classes can be declared and separated by commas.

Public void test (byte a) throws Exception {/throw an Exception try {if (a> Byte. MAX_VALUE/I) // a constant of the Integer class, indicating the maximum value
Throw new Exception ("overflow"); // throws an Exception when overflow occurs.} catch (Exception e) {System. out. println ("exception:" + e. getMessage (); e. printStackTrace ();


How do I determine what exceptions are thrown? For an exception object, the object type is abnormal when the information is really useful, and the exception object itself is meaningless. For example, if the type of an exception object is ClassCastException, this class name is the only useful information. Therefore, when selecting an exception to be thrown, the most important thing is to select the exception class name to clearly indicate the exception.

2. handle exceptions by calling methods

If a method declaration throws an exception, the method that calls it must capture and handle the exception. Otherwise, An Exception error occurs.


After explaining the two methods, we need to think about it. Why should we declare that the method throws an exception? It can be said that whether a method throws an exception is as important as the type of the method return value. If a method throws an exception but does not declare that the method throws an exception, the programmer can call this method without writing code to handle the exception. Then, once an exception occurs, there will be no suitable exception controller to solve the exception, and unexpected unfriendly pages will appear.

It is very convenient to use exceptions, so some programmers are generally no longer willing to write code to handle errors, rather than simply throwing an exception. This is wrong. For completely known errors, you should write the code to handle such errors to increase the robustness of the program. In addition, the exception mechanism is inefficient and should not be used excessively.


The basic knowledge and handling methods of Java exceptions are briefly described above, and there are still some unsatisfactory points to continue to understand!

Zookeeper
Is Java web different from Java ee? Can someone share your experiences after taking the java Road? I 'd better talk about the university situation.

In theory, they differ that javaWeb has a wider scope than javaEE. javaWeb refers to the B/S model system architecture, while javaEE is an enterprise-level project, the so-called enterprise-level project, it is incomparable to some small websites with B/S architecture, but in fact no one will find things to study their differences. The technologies required by javaWeb and javaEE are the same, therefore, Java Web and Java Ee have no difference in practice. They are all projects that develop the B/S architecture. In college, the basics should be laid down, and JavaSE basics must be studied in depth, java ee has several frameworks and is playing with configuration. So it is important to use javaSE. I usually go to the Internet to find some project requirements and try to do some design by myself. I will study the data structure and algorithm more, these will be very useful for future work.

The java tutorial is a bit confused. Who can talk about their understanding of java, or a general understanding of java?

It is a programming language. If you really want to learn JAVA well, you don't just want to learn JAVA. programming requires the accumulation of knowledge from various aspects. So the beginner should first understand the computer knowledge, then I learned the C language and gradually started learning JAVA,
 

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.