Different Java learning (vi) under the inheritance (2.3) exception

Source: Internet
Author: User

Tag: NET operation executes two kinds of log strong blog Data runtime

1, runtimeexception

There is a special subclass exception in exception that RuntimeException Execution.


Assuming that the exception is thrown in the Function's contents, the function can be declared. Compiled as Passed.

Assuming that the exception is declared on a function, the caller is not able to handle it. Compiled as Passed.


The reason for not declaring on a function is that it does not need to be handled by the Caller.

When the exception occurs, you want the program to Stop.

Due to the fact that there is no continuation of the operation at the time of execution,

After you want to 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 do something else. {person p=new person ();p. checkname (null);}

So when you define the exception yourself, assume that the exception Occurs. No further operations can be performed,

Let yourself define exceptions to 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, the exception that is not detected at compile time (exception at execution time). RuntimeException and its subclasses)

instanceof inference is runtimeexception when an exception is entered



2, abnormal-finally

Finally

Like what. Client-to-server links. Run the disconnect operation whether or not an error occurs.

however, the client does not need to process the data read from the server, skipping.

public void Method () {connection database; data operation;//throw new SqlException closes the database;//the action, regardless of whether the data operation is successful or Not. Be sure to close the resource try{connection database, data operation,//throw new Sqlexception}catch (SQLException e) {the database will be handled abnormally;        } Finally{close the database;}}


3. exception-processing Statements Other formats

1,

Try{}catch () {}

2,

Try{}catch () {}finally{}

3,

Try{}finally{//be sure to run the code}

Remember one point: catch is used to handle exceptions.

Assuming no catch means that the exception has not been Processed. Assuming that the exception is a test-time exception, it must be declared.



4, Abnormal-coverage of the abnormal characteristics

1, when the subclass overrides the parent class, it assumes that the method of the parent class throws an Exception. The overriding method of the subclass can only throw exceptions to the parent class or subclasses of the Exception.


Assume This:

Class Zi extends Fu{void show () throws cexception//or Bexception. Not just 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 ());}}


,
Suppose Zi really appears C Exception. well, It's in-house Processing.

2. Assuming that the method of the parent class throws multiple exceptions, the child class only needs a subset of the parent exception when overwriting the Method.

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, try processing is Required. Absolutely not to THROW.




5. Exceptions-practice

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

Illegal values for area Assumptions. Considered to be a failed gain Area.

The problem is represented by an Exception.


6. Exceptions-summary

Abnormal:

What is it? is a descriptive narrative of the Problem. Encapsulates the problem into an object.

Anomaly System:

Throwable

|--error

|--exception

|--runtimeexception

the characteristics of anomaly system: all the classes in the anomaly system and the established objects have the ability to be parabolic,

Which means that it can be manipulated by throw and throwskeyword,

Only abnormal systems have this feature.

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

How to use 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 throw multiple separated by Commas.


Throws an exception object when the function content has a THROW. Try processing is not performed.

Must be declared on the Function. Failed to Compile.

Attention. Except Runtimeexception.

In other words, the runtimeexception exception is assumed in the Function. function can not be declared.

Suppose the function declares an Exception. The caller needs to be processed, and the processing method can be throws to Try.

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

There are two types of exceptions:

Detected exception at compile time

The exception is at compile time, assuming no processing (no throw or try). Compilation failed

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

Execution-time exception (compile-time Misconduct)

At compile Time. No need to deal with it. The compiler does not check.

The occurrence of the Exception. It is recommended not to process, let the program stop, you need to correct the Code.

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

Exception Handling Statements:

Try{//code that needs to be processed;} Catch () {//code to handle exception;} Finally{//will certainly run the code;}

There are three combinations of formats:

1.

Try{}catch () {}

2.

try{}finally{}

3.

Try{}catch () {}finally{}

Attention:

1, finally, The general definition is to close the resource Code. Because the resource must be Freed.


2. finally, There is only one situation that will not Run. When running to System.exit (0);

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

Define your own exceptions:

Define class inheritance exception or RuntimeException

1, in order to let the definition of the class has a parabolic.

2, let the class have the common method of Operation Exception.

When you want to define the information for which you define the Exception. Ability to use functions 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);

}

}

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

Advantages of the Exception:

1. Encapsulate the problem

2, separate the normal process code from the problem-handling code. Easy to Read.

Exception Handling Principles:

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

2, called to throw the exception when the function throws Several. Just deal with a few.

A try corresponding to multiple Catch.

3, multiple catch, and the parent class catch is placed to the most of the Following.

Within the 4,catch, there is a need 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;
} The
assumes that the exception is not handled, but does not belong to the exception that is present with the Feature. The
is able to convert the exception after. Then throws the exception associated with the Feature. The

or exception can be handled when it is necessary to provide an exception-related issue with this feature.
when the caller knows and Processes.

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 that the subclass throws 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. Just try not to throw.
      
view
exceptiontest.java  teacher Computer class
Exceptiontest1.java Graphics Area

Different Java Learning (vi) inheritance under (2.3) 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.