Black Horse programmer-java Base anomaly

Source: Internet
Author: User
Tags define exception throwable

exception : It is not normal. Abnormal conditions that occur when the program is running. is actually the problem that appears in the program. This problem is described in terms of object-oriented thinking and encapsulated as an object. Because of the cause of the problem, a problematic name, a problematic description, and many other attribute information exists. The most convenient way to have multi-attribute information is to encapsulate that information. The exception is that Java encapsulates the problem by object-oriented thinking. This will facilitate the operation of the problem and deal with the problem.

There are a number of problems, such as the angle mark out of bounds, null pointers and so on. To classify these issues. And these problems have common content such as: Each problem has a name, but also the problem of the description of the information, the location of the problem, so you can continue to extract upward. Formed an anomaly system .

--------java.lang.Throwable :

Throwable : can be thrown.

|--error: error, in general, do not write targeted code for processing, usually the JVM occurs, you need to fix the program.

|--exception: exceptions, can be targeted to deal with the way

Whether they are errors or exceptions, they all have specific subclasses that embody each problem, and their subclasses have a common denominator, that is, the parent class name is used as the suffix name of the subclass .

All classes and objects in this system have a unique feature, which is parabolic.

The embodiment of the parabolic: the class and object in this system can be manipulated by throws and throw two key words.

------------------------------------------------------

Class exceptiondemo{

Public static void Main (string[] args) {

byte[] buf = new Byte[1024*1024*700];//java.lang.outofmemoryerror memory overflow error

 }

}

------------------------------------------------------

At the time of development, if the function is defined, there are some problems with the function, and the problem should be marked when the function is defined , so that the caller can give a pre-processing method when the function is used.

How to mark it? By throws keyword completion, format: Throws Exception class name, exception class name ...

After this is marked, the caller, when using this function, must be processed, or the compilation fails.

There are two kinds of processing methods: 1, capturing, 2, throwing.

For capture: Java has a targeted block of statements for processing.

Try

{

code that needs to be detected;

}

catch ( exception class variable name)

{

exception handling code;

}

Fianlly

{

code that is bound to execute;

}

--------------------------------------------------------

catch (Exception e) {//e is used to receive the exception object that the try detects.

System.out.println ("Message:" +e.getmessage ());//Gets the information of the exception.

System.out.println ("toString:" +e.tostring ());//Gets the exception's name + exception information.

e.printstacktrace ();//print exception information in the stack, exception name + exception information + location of exception.

}

---------------------------------------------------------

Exception handling principles: function throws a few exceptions, function call if the try processing, need to correspond with the catch processing code block, such processing is targeted, a few to deal with several.

Special case: When a try corresponds to multiple catch, it must be placed below if there is a catch statement block for the parent class.

Throw The difference between the throws and the keyword:

Throw is used to throw an exception object, followed by an exception object, and a throw is used within a function.

Throws is used to throw exception classes, followed by the exception class name, can be followed by multiple, separated by commas. Throws is used on functions.

Generally: function content if there is a throw, throw exception object, and do not handle, then the function must be declared, otherwise the compilation fails. But there are also special cases.

There are two types of exceptions:

1: The exception that is checked at compile time, as long as the exception and its subclasses are detected at compile time.

2: Run-time exception, where exception has a special subclass RuntimeException, and the subclass of RuntimeException is a run exception, it is said that the exception is not checked at compile time.

The difference between the exception checked at compile time and the run-time exception:

Compile the checked exception is thrown inside the function, the function must be declared, no compilation fails.

Reason for declaration: This exception needs to be handled by the caller.

Run-time exception if thrown inside a function, no declaration is required on the function.

Not declared reason: Do not need the caller to handle, run-time exception occurs, can no longer let the program continue to run, so, do not let the call processing, directly let the program stop, by the caller to modify the code.

When defining a try when you define exception handling, when do you define throws?

If there is an exception inside the function, if internal can be processed, use try;

If the function is not handled internally, it must be declared to be handled by the caller.

Custom Exceptions: When developing a problem that is not defined in Java in the project, then we need to follow the Java exception to establish the idea, the project's unique problems are also encapsulated objects. This exception, called a custom exception.

For division operations, 0 is not possible as a divisor. This problem is described in Java with the ArithmeticException class. For this function, in our project, the divisor can not be a negative number except 0. But the negative part of Java does not have a description. So we need to customize this exception.

To customize the exception steps:

1: Define a subclass to inherit exception or runtimeexception, so that the class has a parabolic nature.

2: Operate by throw or throws.

Abnormal conversion idea: When the exception that occurs is not handled by the caller, it is necessary to convert this exception to an exception that the caller can handle.

try catch finally several ways to combine:

The first type:

Try

 {     

 }

catch ()

 {

 }

The second type:

Try

 {      

 }

finally

 {

 }

The third type:

Try

 {      

 }

catch ()

 {

 }

finally

 {

 }

In this case, if an exception occurs, it is not processed, but the resource must be closed, so the try finally collection is closed only for resources .

Remember:finally is useful for the primary user to close the resource. The resource must be closed, regardless of whether an exception occurred.

system.exit (0); //Exit the JVM, only this case finally does not execute.

When the exception occurs, when the child parent class overwrites, there are some new features:

1: When a subclass overrides a method of the parent class, if the method of the parent class throws an exception, the child class's method either throws an exception or throws the parent class exception or subclass of the exception, and cannot throw other exceptions.

2: If the parent throws more than one exception, the child class can only throw a subset of the exception of the parent class when overridden.

Attention:

If a method in the parent class or interface does not throw an exception, then the subclass is not allowed to throw an exception, and if an exception occurs in the overridden method of the subclass, only try cannot throws.

If this exception subclass can not be processed, has affected the specific operation of the subclass method, at this time can be in the subclass method, through the throw throw RuntimeException exception or its subclasses, so that the subclass of the method is not required throws declaration.

Common exceptions:

1, the Cross-border exception (indexoutofboundsexception) includes arrays, strings;

Null pointer exception (NULLPOINTEREXCEPTION)

2. Type Conversion Exception: ClassCastException

3. No exception for this element: NullPointerException

4, operation exception is not supported;

Black Horse programmer-java Base anomaly

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.