1, Abnormal
1. Abnormal architecture
* java.lang.Throwable * |-----java.lang.Error:Generally, targeted code is not written for processing. * |-----java.lang.Exception:You can handle exceptions * |------Compile time exception(checked) * |-----IOException * |-----FileNotFoundException * |-----ClassNotFoundException * |------Runtime exception(unchecked,RuntimeException) * |-----NullPointerException * |-----ArrayIndexOutOfBoundsException * |-----ClassCastException * |-----NumberFormatException * |-----InputMismatchException * |-----ArithmeticException
2. Look at compile time exceptions and run-time exceptions from the program execution process
Compile time exception: executing javac Possible exceptions during exe naming
Runtime exception: executing Java Exception occurred while naming exe
3. Please give examples of common exception types:
//******************The following are runtime exceptions*************************** //ArithmeticException @Test public void test6(){ int a = 10; int b = 0; System.out.println(a / b); } //InputMismatchException @Test public void test5(){ Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); System.out.println(score); scanner.close(); } //NumberFormatException @Test public void test4(){ String str = "123"; str = "abc"; int num = Integer.parseInt(str); } //ClassCastException @Test public void test3(){ Object obj = new Date(); String str = (String)obj; } //IndexOutOfBoundsException @Test public void test2(){ //ArrayIndexOutOfBoundsException // int[] arr = new int[10]; // System.out.println(arr[10]); //StringIndexOutOfBoundsException String str = "abc"; System.out.println(str.charAt(3)); } //NullPointerException @Test public void test1(){ // int[] arr = null; // System.out.println(arr[3]); String str = "abc"; str = null; System.out.println(str.charAt(0)); } //******************The following are compile time exceptions*************************** @Test public void test7(){ // File file = new File("hello.txt"); // FileInputStream fis = new FileInputStream(file); // // int data = fis.read(); // while(data != -1){ // System.out.print((char)data); // data = fis.read(); // } // // fis.close(); }
2, Exception handling
1. Grab and throw model of Java exception handling
Process 1: "throwing": once an exception occurs during the normal execution of the program, an object corresponding to the exception class will be generated at the exception code.
* And throw this object. * Once the object is thrown, the subsequent code is no longer executed. * * Generation of exception objects:① Exception object automatically generated by the system * ② Manually generate an exception object and throw it( throw)
Process 2: "catch": it can be understood as the exception handling method: ① try catch finally ② throws
2. Exception handling method 1: try catch finally
2.1 instructions:
/* * try{ * //Possible exception codes * * }catch(Exception type 1 (variable name 1){ * //How to handle exceptions 1 * }catch(Exception type 2 (variable name 2){ * //How to handle exceptions 2 * }catch(Exception type 3 (variable name 3){ * //How to handle exceptions 3 * } * .... * finally{ * //Code that must execute * } * * explain: * 1. finally Yes. * 2. try is used to wrap the possible exception code. In case of an exception during execution, an object corresponding to the exception class will be generated. According to the type of this object, it will be matched in catch * 3. Once the exception object in the try matches a catch, it enters the catch for exception processing. Once the processing is completed, it will jump out of the current try catch structure (without writing finally). Continue to execute the subsequent code * 4. catch If the exception type in has no child parent relationship, it doesn't matter who declares it on and who declares it under. * catch If the exception type in satisfies the child parent relationship, the child class must be declared on the parent class. Otherwise, an error is reported * 5. Common exception object handling methods: ① string getmessage() ② printStackTrace() * 6. Variables declared in the try structure cannot be called after the try structure is created * 7. try-catch-finally Structures can be nested */
Summary: how do you view compile time and run-time exceptions in your code?
* Experience 1: use try-catch-finally Handling compile time exceptions means that the program will no longer report errors at compile time, but errors may still be reported at run time. Equivalent to our use try-catch-finally Delay an exception that may occur at compile time until it occurs at run time. * * Experience 2: in development, because runtime exceptions are common, we usually do not write for runtime exceptions try-catch-finally Yes. For compile time exceptions, we say we must consider exception handling. ### 2.2: finally: * 1.finally Yes * * 2.finally What is declared in is the code that must be executed. even if catch Another exception occurred in the, try in return sentence, catch in return Statements, etc. * * 3.Like database connection, I / O flow, network programming Socket And other resources, JVM It cannot be recycled automatically. We need to release resources manually. The resource release at this time needs to be declared in finally Yes.
2.3: [interview questions]
What is the difference between final, finally and finalize?
similar:
throw and throws
Collection s and Collections
String ,StringBuffer,StringBuilder
ArrayList , LinkedList
HashMap ,LinkedHashMap
Rewrite, reload
With different structures:
Abstract classes and interfaces
== , equals()
sleep(),wait()
3. Exception handling method 2:
"throws + exception type" is written at the declaration of the method. Indicates the type of exception that may be thrown when this method is executed.
Once an exception occurs during the execution of the method body, an object of exception class will still be generated at the exception code. When this object meets the exception type after throws, it will be thrown. Exception code and subsequent code will not be executed!
4. Compare the two treatment methods
Try catch finally: the exception is really handled.
Throws simply throws the exception to the caller of the method. It didn't really handle the exception.
5. How to choose two processing methods in development?
* 5.1 If the overridden method in the parent class does not throws Method, subclass overridden methods cannot be used throws,This means that if an exception occurs in a method overridden by a subclass, you must use try-catch-finally Method. * 5.2 Method of execution a In, several other methods are called successively, which are executed in a progressive relationship. We suggest that these methods be used throws In a way. And the method of execution a May consider using try-catch-finally Method.
Supplement:
One of the rules for method overrides:
The exception type thrown by the method overridden by the subclass is not greater than the exception type thrown by the method overridden by the parent class
3, Throw exception manually
1. Instructions
During program execution, in addition to automatically throwing exception objects, we can also manually throw an object of exception class.
2. [interview question]
Difference between throw and throws:
throw indicates the process of throwing an object of an exception class and generating an exception object. The declaration is in the method body.
throws is a way of exception handling, which is declared at the declaration of the method.
3. Typical examples
class Student{ private int id; public void regist(int id) throws Exception { if(id > 0){ this.id = id; }else{ //Manually throw exception object // throw new RuntimeException("the data you entered is illegal!"); // throw new Exception("the data you entered is illegal!"); throw new MyException("You cannot enter a negative number"); } } @Override public String toString() { return "Student [id=" + id + "]"; } }
4, Custom exception class
How to customize an exception class?
/* * How to customize exception classes? * 1. Inherited Exception structures: RuntimeException, Exception * 2. Provide global constant: serialVersionUID * 3. Provides overloaded constructors * */ public class MyException extends Exception{ static final long serialVersionUID = -7034897193246939L; public MyException(){ } public MyException(String msg){ super(msg); } }