Different Java Learning (vi) under the Inheritance (2.6) exception

Source: Internet
Author: User
Tags throw exception

1, RuntimeException

There is a special subclass exception RuntimeException run-time exception in exception.


If the exception is thrown in the function's contents, the function can be passed without declaration and compilation;

If the exception is declared on a function, the caller can pass the compilation without processing.


It is not declared on a function because it does not need to be handled by the caller.

When the exception occurs, you want the program to stop. Because at run time, there is a situation where the operation cannot continue,

After you stop the program, make corrections to the code.

Cases:

Class Person{public void CheckName (String name) {//if (Name.equals ("Lisi"))//is equivalent to nullpointerexceptionif ("Lisi". Equals (name)) Equivalent if (name!=null && name.equals ("Lisi")) System.out.println ("YES"); else System.out.println ("No");}} Main ()              //code shorthand .... To run it yourself add something else {person p=new person ();p. CheckName (null);}

Therefore, when the exception is customized, the operation cannot be resumed if the exception occurs .

Let the custom exception inherit runtimeexception.


Play something interesting,

is not found, an abnormal program will stop.


There are two types of exceptions:

1, the exception that was detected at compile time. (At this point the exception is handled)

2, an exception that is not detected at compile time (run-time exception. RuntimeException and its subclasses)

Abnormal entry will be instanceof to determine whether it is runtimeexception



2, Abnormal-finally

Finally

For example, the client links to the server, regardless of whether an error occurs, to perform a break operation,

However, an error occurs when the client does not need to process the data read from the server, skipping.

public void Method () {connection database; Data operation;//throw new SqlException Close the database;//This action, regardless of whether the data operation is successful, must close the resource try{connection database; data manipulation;//throw new Sqlexception}catch (SQLException e) {The database will be handled abnormally;        } finally{close the database;}}


3. Exception-processing statement other format

1,

Try{}catch () {}

2,

Try{}catch () {}finally{}

3,

try{}finally{//Be sure to execute the code}

Remember one point: Catch is used to handle exceptions. If there is no catch, it means that the exception has not been processed. If the exception is a detection-time exception, it must be declared.


4, abnormal-coverage of the abnormal characteristics

1, when a subclass overrides a parent class, if a method of the parent class throws an exception, the child class's overriding method can only throw the exception of the parent class or the subclass of the exception.


If so:
Class Zi extends Fu{void show () throws cexception//or bexception, not cexception {}}




Class Test {void function (Fu f) {f.show ();}} Class Exceptiondemo7{public static void Main (string[] args) {test t=new test (); T.function (new Fu ());}}


,
If Zi really has a C exception, it is handled internally

2, if a method of the parent class throws more than one exception, the child class overwrites the method as long as a subset of the parent class exception.

3, if no exception is thrown in the method of the parent class or interface, then the subclass cannot throw an exception when overriding the method.

If a subclass has an exception, it must be done with a try and never thrown.



5. Exceptions-Practice

There is a circle and a rectangle that can get the area.

For areas where an illegal value is present, the problem is considered to be the gain area.

The problem is represented by an exception.


6. Exceptions-Summary

Abnormal:

What is it? Is the description of the problem, the encapsulation of the object.

Anomaly System:

Throwable

|--error

|--exception

|--runtimeexception

anomaly System features: All classes in the anomaly system and the objects being built have the ability to be parabolic,

In other words, it can be manipulated by the throw and throws keys,

Only the anomaly system has this feature.

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

The use of throw and throws:

A throw is defined inside a function and is used to throw an exception object.

Throws is defined within a function, used to throw an exception class, and can be thrown multiple separated by commas.


When the function content has a throw throw exception object, try processing is not done. Must be declared on the function, both in the compilation failed.

Note that runtimeexception is excluded. In other words, if the runtimeexception exception is thrown inside the function, the function can be declared.

If the function declares an exception, the caller needs to handle it, and the processing method can throws the try.

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

There are two types of exceptions:

Exception detected at compile time

The exception is compiled, and if it is not processed (without throwing and no try), the compilation fails

The exception is identified, which means that this can be handled.

Run-time exception (not detected at compile time)

At compile time, no processing is required and the compiler does not check.

This exception occurs, it is recommended not to process, let the program stop, the code needs to be corrected.

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

Exception Handling Statements:

try{//code that needs to be processed;} catch () {//code to handle exception;} finally{//code that is bound to execute;}

There are three combinations of formats:

1.

Try{}catch () {}

2.

try{}finally{}

3.

Try{}catch () {}finally{}

Attention:

1. Finally, the close resource code is usually defined. Because the resource must be freed.

2, finally only one situation will not be enforced. When performed to System.exit (0);

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

Custom Exceptions:

Define class inheritance exception or RuntimeException

1, in order for the custom class to have a parabolic.

2, let the class have the common method of operation exception.

When you want to define the information for a custom exception, you can use the functionality already defined by the parent class.

The exception information is passed to the constructor of the parent class.

Class MyException extends Exception

{

MyException (String message)

{

Super (message);

}

}

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

Benefits of Exception:

1. Encapsulate the problem

2, the normal process code and problem-handling code are separated, the party is easy to read.

Exception Handling principles:

1, there are two ways of handling: try or throws.

2, called to throw an exception when the function throws several, on the handle several. A try corresponds to multiple catch.

3, multiple catch, the parent's catch is placed at the bottom.

Within the 4,catch, it is necessary to define a targeted approach. Do not simply define printstacktrace, output statements.

and do not write.

When the exception is caught, the feature cannot be processed and can continue to be thrown in the catch.

Try
{
throw new Aexception ();
}
catch (Aexception e)
{
Throw e;
}
If the exception is not handled, it does not belong to the exception that is present with the feature.
You can convert an exception and then throw an exception that is related to that feature.

Or an exception can be handled when it is necessary to provide an exception-related problem with this function,
When the caller knows and processes. You can also convert a new exception after the catch exception is processed.
Try
{
throw new Aexception ();
}
catch (Aexception e)
{
throw new Bexception ();
}
------------------------------------------
Exception considerations:
When the child parent class overrides:
1, the exception thrown by the subclass must be a subclass or a subset of the exception of the parent class.
2, if the parent class or interface does not throw an exception, the subclass overrides an exception, only try cannot throw.

See
Exceptiontest.java Teacher uses the computer class
Exceptiontest1.java Graphics Area

Different Java Learning (vi) under the Inheritance (2.6) exception

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.