Dark Horse programmer-Java basics-exception system

Source: Internet
Author: User

Exception:ProgramAbnormal during running.

Category: error; Exception

Exception Handling: common operations such as E. getmessage (), E. tostring (), and E. printstacktrace ()

Java provides specific statements for processing:

Try {

To Be DetectedCode;

} Catch (exception variable ){

Code for exception handling; (Handling Method )//If there is a retur statement, it will not be executed later.

} Finally {

The statement that will be executed.System. Exit (0); fianllyNot executed.

}

Exception statement:

When an exception occurs, we recommend that you declare a more specific exception. This process can be more specific.

When the other party declares several exceptions, it should have severalCatchBlock. Do not define unnecessaryCatchBlock. Note that if one of the exceptions declares an exception of the parent class (exception E), it must be placed at the end, otherwise, other exceptions defined will not be run.

Custom exception:

Benefits of exceptions: problems are encapsulated and normal process code and problem handling code are separated for ease of reading.

Class fushuexception extends exception // getmessage ();{This is a custom exception.

// Private int value;

// Fushuexception (){

// Super ();

//}

Fushuexception (string MSG, int value)

{The exception information is defined here. The parent class has already processed the exception information, and the subclass directly obtains the custom exception information through the super (MSG); method.

Super (MSG );

This. value = value; // The exception number can be printed here.

}

Public int getvalue (){

Return value;

}

}

Class demo {

Int Div (int A, int B)Throws fushuexception{// A custom exception is thrown in the function. Declare it in the function.

If (B <0)

Throw new fushuexception("When the divisor is negative ------/by fushu", B );//Manually throw a custom exception object using the throw keyword.

Return A/B;

}

}

Class exceptiondemo3 {

Public static void main (string [] ARGs ){

Demo d = new demo ();

Try {

Int x = D. Div (4,-9 );

System. Out. println ("x =" + x );

} Catch (fushuexception e ){

System. Out. println (E. tostring ());

// System. Out. println ("the divisor has a negative number ");

System. Out. println ("the negative number of the error is:" + E. getvalue ());

}

System. Out. println ("over ");

}

}

The reason for inheriting exception: the exception system has a feature because exceptions and exception objects are not thrown, and they all have the possibility of throwing, which is a unique feature of the throuable system, only classes and objects in this system can be throw and throws.

Difference between throws and throw:

Throws are used in functions; throw is followed by an exception object.

Throw is used in the function; the exception class followed by throws. Can be followed by multiple. Separated by commas.

Runtimeexception exception

If this exception is thrown in the function content, you do not need to declare it on the function. The compilation is the same. If this exception is declared on the function. The caller does not need to process it. Compilation is the same.

The reason is that the caller does not need to handle the issue. When this exception occurs, you want the program to stop. Because the operation cannot be continued during running, you want to modify the code after stopping the program.

Custom exception: If this exception occurs and you cannot continue the operation, the custom exception inherits runtimeexception.

 

Class lanpingexception extends exception {

Lanpingexception (string message ){

Super (Message );

}

}

Class maoyanexception extends exception {

Maoyanexception (string message ){

Super (Message );

}

}

Class noplanexception extends exception {

Noplanexception (string MSG ){

Super (MSG );

}

}

Class computer {

Private int state = 3;

Public void run () throws lanpingexception, maoyanexception {

If (State = 2)

Throw new lanpingexception ("blue screen ");

If (State = 3)

Throw new maoyanexception ("smoke ");

System. Out. println ("computer running ");

}

Public void reset (){

State = 1;

System. Out. println ("computer restart ");

}

}

Class teacher {

Private string name;

Private Computer cmpt;

Teacher (string name ){

This. Name = Name;

Cmpt = new computer ();

}

Public void prelect () throws noplanexception {

Try {

Cmpt. Run ();

} Catch (lanpingexception e ){

Cmpt. Reset ();

} Catch (maoyanexception e ){

Test ();

Throw new noplanexception ("class cannot continue" + E. getmessage ());

}

System. Out. println ("Lecture ");

}

Public void test (){

System. Out. println ("exercise ");

}

}

Class exceptiontest
{
Public static void main (string [] ARGs)
{
Teacher T = new teacher ("instructor bi ");
Try
{
T. prelect ();
}
Catch (noplanexception E)
{
System. Out. println (E. tostring ());
System. Out. println ("Changing teachers or holidays ");
}
}
}

The exception is overwritten in the Child parent class:

1) when a subclass overwrites the parent class, if the method of the parent class throws an exception, the override method of the subclass can only throw the exception of the parent class or the subclass of the exception.

(The parent class already has a problem. To Inherit the function of the parent class, the Child class cannot be more problematic than the parent class, but can only be more optimized.

2) If the parent class throws multiple exceptions, when the subclass overrides this method,Only a subset of parent class exceptions can be thrown.(As longCan be processed by the parent class ).

3) IfParent classOrInterfaceIf no exception is thrown in the method, the subclass cannot overwrite the method,OnlyTry,Never throw.

Package (Package):

Class Classification Management; multi-layer namespace provided for the class; written in the first line of the program; full name of the class: package name.Class Name. A package is also an encapsulation. you can separate the source file from the class file by creating a package. Encryption and Protection

Source codeGain a peek.

Only public and protected permissions can be used between packages.

Public protected default private

In the same classOK OK

 

OK

 

SubclassOK

 

In different packagesOK

Import package: Use the import keyword

Related Article

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.