What is an exception:
In real life, there are always some problems, focus on life, we will have work problems, such as job dissatisfaction, low salary, work without development prospects, then we will become this is a "problem."
In the Java world, then we correspond to the program, then the program has one or the other problem, then we become "abnormal". Exceptions are anomalies that occur when the program is running .
Java encapsulates an exception into an object's description, which is highly generalized.
There's nothing in Java we can do. Let's just talk about exception.
Categories of Exception:
1. Compile-time exceptions must be captured
2. Run-time exception (i.e. runtimeexception)
Java Exception Solution
Class Demo
{
public int Division (int a,int b) throws exception//declares that the program may have an exception that is thrown to the caller for processing
{
return a/b;
}
}
Class Arithmeticexceptiondemo
{
public static void Main (string[] args) throws exception//If an exception occurs, it is addressed to the caller. The main function is called the Java Virtual machine, so it is the virtual machine
{//is handled with default occurrences
Demo D=new demo ();
D.division (3,0);
}
}
Summary: The throws exception is dropped to the caller for processing.
try{} catch{} method to resolve
1 classFushuexceptionextendsException2 {3 fushuexception (String msg)4 {5 Super(msg); 6 }7 }8 classDemo9 {Ten Public intDivision (intAintb) One A { - - Try the { - if(b<0) - Throw NewFushuexception ("Negative number exception" +b); - } + Catch(fushuexception e) - { +System.out.println ("This is a negative number exception"); A } at - - returnA/b; - - } - } in classExceptionDemo4 - { to Public Static voidMain (string[] args) + { -Demo d=NewDemo (); the intX=d.division (3,-1); *System.out.println ("x=" +x); $System.out.println ("Over");Panax Notoginseng } -}
Summary: Throw out the exception object yourself, detect and capture your own solution
We can also throw this to the caller to resolve
1 classFushuexceptionextendsException2 {3 fushuexception (String msg)4 {5 Super(msg); 6 }7 }8 classDemo9 {Ten Public intDivision (intAintbthrowsfushuexception One A { - - the - if(b<0) - Throw NewFushuexception ("Negative number exception" +b); - + - +System.out.println ("This is a negative number exception"); A at - - returnA/b; - - } - } in classExceptionDemo4 - { to Public Static voidMain (string[] args) + { -Demo d=NewDemo (); the Try * { $ intX=d.division (3,-1);Panax NotoginsengSystem.out.println ("x=" +x); - } the Catch(fushuexception e) + { ASystem.out.println ("This is a negative number exception"); the System.out.println (e.tostring ()); + } - $System.out.println ("Over"); $ } -}
This is the basic rule of Java exception handling, you either throw it or solve it yourself, as you do with the math problem in life, you either jump over it, or you solve it yourself.
On the principle of exception handling in Java