A
Exception Exception---> meaning is the exception.
One: Exception handling mechanism code and program source code put a piece of the bad 1)--Logic code and error handling code together to reduce the readability of the program
2)---> Programmers have many exceptions to be considered when writing programs, requiring high
Two: The mechanism of abnormal mechanism----When the program has an error and how the program exits safely.
Java's exception mechanism is the Throwable class in the JDK as the parent class all possible unexpected situations
Three: Java in an object-oriented way to handle exceptions---process--->1) throws an exception: Execute code, an exception occurs, produce an object that represents the exception, stop the current code, and pass the exception to the JRE processing (JVM)
------->2) Catch exception: After the JRE gets an exception, look for the appropriate code to handle.
Four: Exception classification
--->error (uncheckedexception) Java operating system internal error or resource exhaustion. For example, the JVM crashes and loses power. Programmers can't handle
Throwable "
--->exception--->1) runtimeexception (uncheckedexception) compile without error, run times wrong. Example: Divisor is 0
--->2) non-runtimeexception (checkedexception) Compile times error, ★[need to use exception mechanism to handle exceptions]
Runtime exception (run exception handling mechanism code and program source code put a piece of the bad 1)--Logic code and error handling code together to reduce the readability of the program timeexception) [Uncheckedexception]---> The programmer's own logic is wrong, and the exception can be handled by modifying the code. Force stop program to run, easy for programmers to modify, enhance program "security"
Non-runtime exception, compile exception [checkedexception]---> Need to handle exceptions through the exception mechanism, enhance the "robustness" of the Code
The downside of dealing with exceptions and putting together useful code
1 Logic Chaos
2 High requirements for programmers themselves
Java Methods for handling exceptions
1: Throws an exception.
2: Catch exception.
Two
The first approach: Throw an exception, catch an exception (try Catch)
Format
try{
Source code that could produce an exception
}catch (Exception e) {
Handling Exception methods
}finally{
Freeing resources
}
Syntax features 1) A try statement must have at least one catch{} or finally{} block
2) catch (object) if there are multiple catch, then object from top to bottom---> "small (subclass) on top, big (parent class) Next"
3) A try statement can produce multiple catches, a catch statement can catch only one exception object
4) Common methods in Catch blocks--->a) toString ()--Displays the class name of the exception class and the cause of the exception
---->b) GetMessage ()--displays only the cause of the exception and does not show the exception class name
---->c) Printstacktrace ()--Displays the class name of the exception class, causes the exception, and prints the stack heap information (common)
Three
The second approach: automatically throws an exception--->throws clause
Format:
Method Name + throws Exception class () {
Source code with possible exception
}
Features that automatically throw exceptions--->1) when a compile-time exception (checkedexcption) occurs, it is not necessarily handled, you can throw an exception up and let the method that calls it handle it. It can be thrown up and processed in the main method, that is, when the program runs.
--->2) If multiple exceptions are thrown in a method body, the exception class is separated by "," after the method name
Principle of declaring exceptions in method overrides (1)--The parent class method does not declare an exception, and the subclass override method cannot declare the exception
(2) The exception class that the overridden method throws, and the exception class relationship thrown by the overridden method. Subclass <= Parent Class (Inherits parent-child)
(3)---> Subclass throws the number of exception classes, no extra parent throws the number of exception classes (not the number, refers to the type)
Four
Manually throw exception class throw
Step: 1) Find a suitable exception class
2) Create objects of this class
3) manually throw the object
4) The code after the throw is not executed.
Example---> Throws an exception in the method body. Throw
public static int sum (int a, int b) {
if (b==0) {
throw new Myzeioexception ();
} else {
int sum=0;
sum=a/b;
return sum;
}
Example--->
public static void Main (string[] args) {
SDF ();
System. Err. println ("SXF"); Do not run
}
public static void Sdf () {
System. Out. println ("Paymentrequestserviceimpl.sdf ()"); Run
int a =1/0;
System. Err. println ("tt"); Do not run
}
Example--->
public static void Main (string[] args) {
try {
SDF ();
} catch (Exception e) {
TODO auto-generated Catch block
E. Printstacktrace ();
}
System. Out. println ("Paymentrequestserviceimpl.main (SXF)"); Run
}
public static void Sdf () throws exception{
System. Out. println ("Paymentrequestserviceimpl.sdf ()"); Run
if (true) {
throw new Exception ();
} else {
System. Out. println ("Paymentrequestserviceimpl.sdf (dddd)"); Do not run
}
System. Out. println ("Paymentrequestserviceimpl.sdf (STTTT)"); Do not run
}
Five
Fourth method:---> Custom exception classes
When to customize the exception class---> When the Exception class in the exception mechanism in the JDK does not meet the exceptions that the programmer can produce, the exception class needs to be self-built
* Fourth Implementation method Custom exception
* When the system provides Exception does not meet our own
* When it comes to the need for a personalized speech
* 1. Create an exception subclass
* 2, creation of two constructors
* 3, parameters of the constructor can be assisted by the
* SUPER (parameter) to call the parent class.
* Constructors with parameter parameters
1, under normal circumstances inherit exception or the following subclass (except RuntimeException)---"need to process mycheckexception at compile time (most custom exception class inherits exception)
2, Inherit RuntimeException class ======= "in the Run time processing runtimeexception (less and less)
Example:
Custom exception Classes
public class Myzeioexception extends Exception {
Public myzeioexception () {}
Public myzeioexception (String str) {
Super (STR);
}
}
Test class
public class test{
public static void Main (String args) {
try{
SUM (2,0);
}catch (Myzeioexception e) {
E.printstacktack ();
}
}
public static int sum (int a, int b) throws Myzeioexception {
if (b==0) {
throw new Myzeioexception ();
} else {
int sum=0;
sum=a/b;
return sum;
}
}
Design and reconstruction of exception handling Learning II