Exception handling and common classes

Source: Internet
Author: User

Three kinds of exceptions:

Check Exceptions: User error actions or problems that cannot be foreseen by the programmer must be handled by the Java language.

Runtime exception: A runtime exception is an exception that can be avoided by programmers, such as an array out-of-bounds, a divisor of 0, and a reference to null.

Error: The error is not an exception, a problem that the programmer or user cannot control, such as a call stack overflow.

Abnormal control Flow:

An exception is an object that is thrown by a method and can be handled in three ways:

1. Catch this exception 2. Catch the exception and throw it down 3. Do not capture let it pop up to the call stack.

We can use the Printstacktrace () method of the Throwable class to print the call stack trace information.

Thowable class:

All exceptions inherit from the Throwable class, so the methods of the Throwable class can be called on any caught exception.

Exception handling and claim rules:

Use Try...catch to handle exceptions or use throws to declare exceptions to be thrown in a method, or you can use throw to actively throw an exception. When you use catch, you write the subclass exception in front. Subclasses cannot throw more exceptions than the parent class.

Common classes:

1. System-related classes

The System class properties in, out, and err correspond to standard input, standard output, error stream output, respectively

System Class Common methods: Currenttimemillis (), exit (), GC ()

Runtime class represents the run-time environment of a Java program

2. String-related classes

String class

The StringBuffer class is used to modify the string without generating a new object, which is thread safe.

The Stingbuilder class is used without regard to thread safety, and is much more efficient than stringbuffer.

3. Date-related classes

Date class

DateFormat abstract class and SimpleDateFormat class

Calendar abstract class and GregorianCalendar class

4. Mathematical Operations related classes

The math constants and methods commonly used in the math class are internal to the class and are static types, and can be called through the Math. Method Name ().

Random number 1. System.currenttimemillis () to get a long number with a current time-millisecond number

2. Using the Math class static method random () returns a double value of 0 to 1, which can be multiplied by 100 to make it a random number within 100.

3. Generate a random number from the random class. Generally use this more professional.

5. Packing class and automatic packing and unpacking

In order to work with the basic data types using object mode, Java encapsulates the basic data types into classes and becomes wrapper classes.

The eight basic data types correspond to different wrapper classes, such as int corresponding to integer,float for float.

          

    

          

  

Exception handling and common classes

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.