Dark Horse programmer-exception

Source: Internet
Author: User

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you!

Exception.

1,
Exception:

That is, some exceptions in the Java program. The Java language describes these exceptions and encapsulates the objects.

2. Exception System: A system is formed when there are many exceptions.
A. throwable
| -- Error: It is critical and cannot be handled. You must modify the program directly.
| -- Exception: You can define targeted processing.
Generally, the suffix of the subclass name is the parent class name.
B. The biggest feature of this exception system: the classes and objects in this system are throwing.
C. By default, the JVM exception handling method is to print the received Exception name, information, and location on the console and end the program.

D. Methods in throwable. Exception does not have a special method. They all inherit the methods in throwable.
String getmessage (): obtains the exception information.
String tostring (): overwrites the name and information of an object.
Void printstacktrace (): prints the exception information, name, and location.
3. Exception Handling: There are two ways to handle eception exceptions.
First, the declaration throws. Inform the caller that the function is faulty. Use the throws keyword to declare the problem in the function.
Requirement: the Division by zero is incorrect.
Type 2: capture. You can use the captured code block of a real object.
Try {code to be detected}
Catch (exception variable) {Exception Handling Code}
Finally {code to be executed}
4. Create an exception object by yourself: You can customize the exception information, but you need to manually throw the exception.
Thrwo: manually throw an exception. 

5. What are the differences between the throws and throw keywords?
Throws are used in functions and are used to declare an exception. Multiple exception classes can be thrown later, as long as they are separated by commas.
Throw can only be used in functions to throw exception objects. Feature: once an exception is executed, the function is terminated.
6. Custom exceptions:
A. Overview of custom exceptions
Some exceptions are not described in Java. For example, the divisor cannot be a negative number.
In this case, we need to describe this problem according to the object-oriented idea and encapsulate it into an object.

B. Definition of exceptions:
1. Define a class to describe the problem.
2. This class must inherit the exception class and be throttled.

7. There are two types of exceptions.
First, exception during compilation: exception detected by the compiler during compilation.
Usually, you need to analyze the specific processing code for processing.
Type 2: runtime exception runtimeexception: an exception that is not detected during compilation. This exception occurs and is not included in the check during compilation.

This exception does not need to be declared on the function. Even if it is declared, the caller does not need to provide a pre-processing method because it will not cause compilation failure. Generally, no specific code is written for processing. Once this happens, stop the program. To correct the code. For example, an exception occurs when the badge is out of bounds.
Distinction: runtimeexception and its subclass are both runtime exceptions.

Code Demonstration:
Exception Handling Method 1: declare and throw.

Public class exceptiondemo {
Public static void main (string [] ARGs) throws arithmeticexception {
/*
* Requirement: An Exception Handling Method with a division of 0.
* Method 1: declare and throw the function. Tell the caller that an exception may occur.
*/
Demo d = new demo ();
Int x = D. Div (4, 0 );
System. Out. println ("x =" + x );
System. Out. println ("over ");
}
}

// Customize a class. This class has a method for Division calculation.
Class demo {
Int Div (int A, int B) throws arithmeticexception {// throw.
Return A/B;
}
}
Exception Handling Method 2: capture and handle exceptions.

Public class exceptiondemo2 {
Public static void main (string [] ARGs ){
Try {
Demo2 d = new demo2 ();
D. Div (4, 0 );
}

// Exception handling method.
Catch (arithmeticexception e ){
System. Out. println ("message:" + E. getmessage (); // print the information.
// System. Out. println ("tostring:" + E. tostring (); // Exception name + information.
// System. Out. println ("Ah, exception ");
// E. printstacktrace (); // print the exception information + name + location.
}
System. Out. println ("over ");
}
}
Class demo2 {
Int Div (int A, int B) throws arithmeticexception {
If (B = 0)
Throw new arithmeticexception ("finished, excluded ");
Return A/B;
}
}

Custom exception:

Public class exceptiondemo3 {

Public static void main (string [] ARGs ){
/*
* Requirement: An exception occurs when the custom divisor cannot be a negative number.
* Definition method:
* 1. Define a class to describe the exception.
* 2. This class must inherit the exception class and be throttled.
*/
Try {
Demo3 d = new demo3 ();
Int num = D. Div (4,-1 );
System. Out. println (Num );
}
Catch (arithmeticexception e ){
System. Out. println ("message =" + E. getmessage ());
}
Catch (fushuexception e ){
System. Out. println ("message =" + E. getmessage ());
System. Out. println ("tostring:" + E. tostring ());
System. Out. println ("negative number:" + E. getnum ());
E. printstacktrace ();
}
}
}

// An error occurred while describing the negative number.
Class fushuexception extends exception {
Private int num;
Fushuexception ()
{
Super ();
}
Fushuexception (string message ){
Super (Message); // sends the exception information to the parent class for processing.
}
Fushuexception (INT num ){
This. num = num;
}
Public int getnum (){
Return num;
}
}
// Division operation of two integer numbers.
Class demo3 {
Int A, B;
Int Div (int A, int B) throws arithmeticexception, fushuexception {
If (B <0)
Throw new fushuexception (B );
// Throw new fushuexception ("error. The divisor is not allowed to be negative .. ");
If (B = 0)
Throw new arithmeticexception ("the divisor is zero .. ");
Return A/B;
}
}

Http://edu.csdn.net/heima Android Training
Http://edu.csdn.net/heima; Java training is coming to communicate with you! See http://edu.csdn.net/heima for details

 

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.