First, the basic knowledge
An exception is an object used to describe an exception that occurs in the code. An exception object is thrown when a condition that causes an exception occurs. Method can choose to handle an exception yourself or continue to throw an exception. Exceptions can be generated by the Java runtime system or manually. Exceptions thrown by Java are related to underlying errors that violate language rules or enforce environment constraints, which are typically used to report certain error conditions to callers of a method.
Ii. type of exception
All exception types are subclasses of Throwable. Throwable at the top of the exception class hierarchy, followed by the two branches is exception and error. The exception can be used for capture or for custom exceptions. Error defines an exception that cannot be caught by a program in a normal environment, usually in response to a catastrophic failure.
Third, catch the exception
class Solution { publicstaticvoid main (string[] args) { = "ABC"; try { int a = integer.parseint (s); System.out.println (a); Catch (NumberFormatException exc) { System.out.println ("Unknown integer format"); }}}
When the exception is thrown, the program is immediately transferred from the try to the catch. Statements after the exception position are not executed.
ImportJava.util.Scanner;classSolution { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); String s=In.nextline (); String T=In.nextline (); Try { intA =Integer.parseint (s); intb =Integer.parseint (t); System.out.println (A/b); } Catch(ArithmeticException exc) {System.out.println ("Divisor cannot be 0"); } Catch(NumberFormatException exc) {System.out.println ("Unknown integer Format"); } }}
A single block of code can have multiple exceptions, and you can catch all possible exceptions by specifying multiple catch statements. When an exception is thrown, each catch statement is checked sequentially until it encounters a catch statement that can match, and then ignores the remaining catch statements. If the exception cannot encounter a matching catch statement, it will not be caught and the program will be interrupted by an exception.
When multiple catch statements are used, the subclass exception capture must precede all the superclass. If a superclass exception capture precedes a subclass, then the subclass exception will never be caught.
Iv. built-in exceptions
In the Java built-in exception class, the most common is the RuntimeException subclass.
All methods do not need to indicate that these exceptions will be thrown, known as unchecked exceptions.
Iv. Custom Exceptions
The exception class does not define any methods, but inherits the methods provided by Throwable.
You can implement a custom exception by inheriting exception, and you can modify the description of the exception by overriding the ToString method.
ImportJava.util.Scanner;classMyExceptionextendsException {myexception (String msg) {Super(msg); } @Override PublicString toString () {return"MyException:" +Super. GetMessage (); }}classSolution {Static intDivideintAintbthrowsMyException {if(b = = 0) Throw NewMyException ("Divisor cannot be 0"); returnAb; } Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intA =In.nextint (); intb =In.nextint (); Try { intres =Divide (A, b); System.out.println (RES); } Catch(MyException exc) {System.out.println (exc); } }}
Five, chain-type exception
With chained exceptions, you can associate another exception with an exception. The second exception is the cause of the first exception, known as throwing an exception or behind an exception. The Getcause method can be used to get the exception behind the current exception, which is associated with an exception through the Initcause or constructor, and can only be set once for the back exception.
classSolution {Static voidThrowera () {indexoutofboundsexception exc=NewIndexoutofboundsexception ("Index Out of Bounds"); Exc.initcause (Newnullpointerexception ()); Throwexc; } Static voidThrowerb ()throwsException {Throw NewException ("Null pointer cause Exception",Newnullpointerexception ()); } Public Static voidMain (string[] args) {Try{Throwera (); } Catch(Indexoutofboundsexception exc) {System.out.println (exc.tostring ()); System.out.println (Exc.getcause (). toString ()); } Try{throwerb (); } Catch(Exception exc) {System.out.println (exc.tostring ()); System.out.println (Exc.getcause (). toString ()); } }}
Vi. multiple captures
Multiple exceptions can be captured by a separate catch statement.
ImportJava.util.Scanner;classSolution { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); intA =In.nextint (); intb =In.nextint (); int[] Nums = {1, 2, 3}; Try { intres = A/b; Nums[a]=Res; } Catch(ArithmeticException |arrayindexoutofboundsexception exc) {System.out.println (exc.tostring ()); } }}
Java Notes: Exception handling