<> abnormal

<>1 summary

Exceptions are objects that encapsulate error information
It is determined by the type of exception , Prompt information , There are three parts of the error reporting line number prompt

<>2 Exceptional inheritance structure

<>3 Exception handling

When an exception is encountered in the program , There are usually two ways to deal with it : Catch or throw up
When a method throws an exception , Call location can continue to throw up without processing , You can also catch handling exceptions
You can combine the examples in your life : If you encounter problems at work , We can choose to deal with it ourselves ( capture ), Or give it to the superior ( Throw out )
<> Capture mode :

<> Throw mode :
Add code to methods that may have exceptions :
throws Exception type
for example :public static void main(String[] args) throws Exception{ }

<>4 practice : Abnormal test

create package : cn.tedu.exception
Create class : ExceptionDemo.java
package cn.tedu.exception; import java.util.InputMismatchException; import java
.util.Scanner; /** This class is used for testing JAVA Introduction cases of exception in */ public class ExceptionDemo { //1. Create entry function
public static void main(String[] args)throws Exception{
//method();// call method()-- Abnormal exposure //method1();// call ,method1()-- Scheme 1 , Exception capture
//method2();// call ,method2()-- Scheme 2 , Exception throw f();
// establish f() It's for standard writing ,main() Catch handling exception before calling , You can't throw it to me main() } public static void f() { try {
method2(); } catch (Exception e) { System.out.println(" Please input the correct data "); } }
// establish method2(), practice BUG Solution 2 /** summary 8: * If the method throws an exception , So who calls this method , You need to resolve the exception thrown by this method ( Capture solution / Continue to throw )
* therefore main() Called method2(), If main() No capture processing , You also need to throw an exception *
But pay attention : We usually do it in the main() Catch resolve exception before calling , Instead of throwing the problem at the table main() * Because no one has solved it * */ //public static void
method2() throws InputMismatchException,ArithmeticException,Exception{ public
static void method2() throws Exception{ //1. Copy the code that just might have an exception System.out.println(
" Please enter the first data you want to calculate :"); int a = new Scanner(System.in).nextInt(); System.out.println(
" Please enter the second data you want to calculate :"); int b = new Scanner(System.in).nextInt(); System.out.println(
a/ b ); } //4. establish method1(), practice BUG Solution 1 /** Syntax rules of exception capture : * try{ * Abnormal code may occur *
}catch( Exception type Exception parameter name ){ * If the corresponding solution of the exception is caught * } * */ public static void method1() {
//5.1 Follow the capture syntax try-catch structure /** summary 4:try Put in the code may be abnormal */
/** summary 5: If an exception occurs , It'll be catch Captured , Execute the matched solution */ try { //5.2 Copy the code that just might have an exception System.out.
println(" Please enter the first data you want to calculate :"); int a = new Scanner(System.in).nextInt(); System.out.
println(" Please enter the second data you want to calculate :"); int b = new Scanner(System.in).nextInt(); System.out.
println( a / b ); }catch(InputMismatchException e) {// Exception capture 1 System.out.println(
" Incorrect input type , Please enter the correct integer ~"); /** summary 6: Because there may be a variety of exceptions in the program , therefore catch It can be used for many times */ }catch(
ArithmeticException e) {// Exception capture 2 System.out.println(" Divisor cannot be 0~");
/** summary 7: If there are any exceptions other than the above two , use exception Match capture * This is the most classic use of polymorphism , We don't care about subclass types *
As long as it's a solvable exception , All of them Exception Subclass of , Polymorphism treats these exceptions as parent types * And then capture , Using common exception resolution to solve * */ }catch(
Exception e) {// Exception capture 3- General solution System.out.println(" Please input the correct data ~"); } }
//2. establish method() It's used to detect human exposure public static void method() { //3.1 Prompt and receive user input data System.
out.println(" Please enter the first data you want to calculate :"); int a = new Scanner(System.in).nextInt(); System.
out.println(" Please enter the second data you want to calculate :"); int b = new Scanner(System.in).nextInt();
//3.2 Calculate the result of the output division operation // input 8 and 2.2, report errors :InputMismatchException[ Input mismatch exception ]
// input 10 and 0, report errors :ArithmeticException[ Arithmetic abnormality , Divisor cannot be 0] System.out.println( a / b );
/**1. Don't be scared BUG, The real warrior dare to face what he wrote BUG*/ /**2. Learn to read the wrong information in the newspaper , Determine your wrong direction */
/**3. Learn to read the wrong information in the newspaper , Where to report mistakes * be careful : The source code can't be wrong , What we need to see is our own code */ } }
Quick key small supplement :
Ctrl + Shift + T : stay eclipse Find the class you want to find
Ctrl + T : View the inheritance structure of a class
Ctrl + O : View class structure information ( outline )

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree