Exception
There are two main categories of abnormal situations in the program: Error (Errors) and exceptions (Exception)
Error: The program itself cannot handle this abnormal situation, it will cause the program to crash;
Example: Memory exhaustion error, JVM crash error, etc.
Exception (Exception): The program can be special code, the abnormal situation to capture and processing, the program can continue to run;
Example: except 0 exception, null pointer exception, array subscript out of bounds exception
Catching exceptions
Syntax format 1:try{//Possible exception code}catch (Exception class variable name) {//The processing code after an exception occurs//printstacktrace (); The method outputs the name of the exception information variable in the stack memory. Printstacktrace (); Syntax Format 2:try{//Code}finally{//finally block that could have an exception: a statement that typically frees resources, such as closing a stream, closing a connection to a database}finally: Regardless of whether the code in the try has an exception, Executes the code syntax format in the finally block 3:try{}catch (Exception class variable name) {}finally{//Even if a return statement appears in a try block or catch block, the finally block will still execute// If a system.exit (int) is present in a try block or catch block, the finally block will not execute} multiple catch blocks: Exception classes in multiple catch blocks must be arranged from small to large, sibling exception classes try{}catch (Child exception class Variable name {}catch (Child exception class variable name) {}catch (Parent exception class variable name) {} ... [Finally{}]try, catch, and finally cannot exist alone, but try and catch, try, and finally can be used together, and catch and finally cannot be used alone
declaring exceptions
Syntax format:[access modifier] Return value type method name () throws exception class {}
Throw Exception
syntax format: throw exception object;
Example:package exception;/** * demonstrating exception declarations and throws * * @author Genius Federation - Yukun */public class exceptiondemo {//declares the method M1, and declares that the method will appear with an exception of type RuntimeException public void  M1 () throws RuntimeException {/* * throws a runtimeexception type of exception, not using Try-catch capture * uses the way that the exception is declared, handing the exception thrown here to the caller m2 processing * declaring the exception type, which must be the exception type thrown or its parent type */throw new RuntimeException ();} Declares the method M2, and declares that the method will appear with an exception of type Exception public void m2 () throws exception {try {// The call declares a method m1// the runtimeexception exception and uses Try-catch to handle exceptions thrown in M1 M1 ();/* Because the catch block is caught by the exception type match, The method M1 declares the runtimeexception, so only variables of the runtimeexception or runtimeexception parent type can be declared in the catch's parentheses, and the RuntimeException subclass variable cannot be declared. Because it doesn't guarantee an exception. */} catch (Exception ex) {// throws a Exception type exception// Try-catch is not used for capture processing, so the method must also declare exception throw new exception ();} finally {/* * code in a finally block executes regardless of whether the code in the try and catch blocks throws an exception* code in a finally block executes even if a return statement is executed in a try or catch block * unless system.exit (int) is executed in a try or catch block; * Or an error causes the JVM to crash and finally will no longer execute the */}}// method m3public void m3 () {try {/* * The call declares the method m2 for the exception exception, and M3 does not declare the exception, * so it must use the Try-catch block handling method m2 The resulting exception */m2 (); catch (Exception ex) {/* * throws an exception of type Exception, and does not capture with Try-catch, nor does it declare an exception; * But the return statement is used in the finally block, so the code compiles and executes */throw new exception () normally;} The return statement used in the finally {/* * finally block overwrites the exception of the exception type thrown in the catch block; * ; * So the method can neither declare exceptions nor use Try-catch to handle exceptions thrown in catch blocks; * generally not recommended for this use */return;}} Public static void main (String[] args) {ExceptionDemo t = new Exceptiondemo (); T.m1 ();}}
Custom Exceptions
Inheriting subclasses of exception or exception
Example:/*** Demo Custom Exception class * @author Genius Federation-Yukun */public class Myexceptiondemo extends exception{public static void main (string[] args) {try {throw new MyException ();} catch (MyException e) {//Call Printstacktrace method E.printstacktrace ();}}} /*** Creating a custom exception class MyException, inheriting Exception class or Exception subclass * @author Genius Federation-Yukun */class MyException extends exception{/** * Override Printstacktrace in parent class */@Overridepublic void Printstacktrace () {System.out.println ("Congratulations, in my custom exception! ");}}
This article from the "Genius Union Education Official Blog" blog, reproduced please contact the author!
I genius official free Tutorial 38: Java Basic Teaching exception