In this article we will share an introduction to Java Exceptions and Java exception handling.

Introduction to Java Exceptions

1. Type conversion exception: ClassCastException

2. ArithmeticException: ArithmeticException

In Java, some abnormal phenomena are abstracted to form some exception types.

All exceptions are present in the getMessage()/printStackTrace() methods of the Throwable class.

Compile-time exceptions, also known as checked exceptions, are exceptions that must be preprocessed before compilation. This exception does not occur at compile time; all exceptions occur at run time. Just as there must be fire extinguishers and fire hydrants in the corridor, if there is no such equipment, there is no safety certificate, without this safety certificate, enterprises can not produce. Companies must have pre-emptive measures for fires, which are not currently occurring.

Syntax check is carried out during compilation. Java syntax has many lines, one of which is: the checked exception must be preprocessed.

package com.wkcto.chapter02.demo01; /** * Runtime exceptions are subclasses of the RuntimeException class * no preprocessing is required, Public class Test01 {public static void main(String[] args) {int x = 10; int y = 2; // divide(x, y); //10/2=5 // divide(10, 0); Divide22 (x, y); divide22(x, y); divide22(10, 0); System.out.println("main.... end..." ); } public static void divide(int num1, int num2) {int result = num1/num2; System.out.println( num1 + "/" + num2 + "=" + result); } / / definition method, calculation of two integers, judging by divisor of 0 avoid arithmetic exception public static void divide22 (int num1. Int num2) {if (num2 == 0) {system.out.println (" divisor cannot be 0"); return ; } int result = num1/num2; System.out.println( num1 + "/" + num2 + "=" + result); }}Copy the code
package com.wkcto.chapter02.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; Public class Test02 {public static void main(String[] args) {d:/abc.txt FileInputStream fis = new FileInputStream("d:/abc.txt"); Throws a FileNotFoundException exception (FileNotFoundException); throws a FileNotFoundException exception. Exceptions declared through throws // Exceptions checked must be preprocessed before compilation; otherwise, compiler syntax will report errors}}Copy the code

Java Exception Handling

● Throws processing

The try… Catch processing

Grammar:

try{

Review code that may generate exceptions

As soon as one of the statements in the try block raises an exception, the program immediately jumps to the catch clause, and the code after the try block stops executing

In a try code block, there may be multiple statements with checked exceptions that need to be preprocessed, which can be caught by multiple catch clauses

}catch(exception type 1 e1){

Catch the exception of exception type 1, catch the exception, in the development stage, the general way of preprocessing is to print the exception on the screen, programmers can debug the program according to the exception information

e1.printStackTrace(); // Every exception has this method

}catch(exception type 2 e2){

If the catch clause catches an inherited exception, if you want to preprocess the exception separately, you should catch the child first, then the parent, or catch a parent directly

}finally{

The finally clause is always executed regardless of whether an exception is generated

System resources are often released in the finally clause

}

package com.wkcto.chapter02.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; If no exception is caught, the system will interrupt, after the generated exception is caught, the program will not interrupt, will continue to execute the function of the exception handling: * Improve the robustness of the program, Public class Test03 {public static void main(String[] args) {try{FileInputStream fis = new FileInputStream("d:/abc.txt"); System.out.println(" creates a channel between the current program and the specified file, "); fis.read(); // IOException is detected in the read() method; system.out.println (" read a byte from file ") is preprocessed. fis.close(); //close() IOException = system.out.println (" close the channel after reading "); }catch (FileNotFoundException e) {e.printStackTrace(); // The generated exception is captured, and the program does not interrupt, }catch (IOException e) {// Catch (IOException e) {// FileNotFoundException inherits IOException. } system.out.println ("main... ");} system.out.println ("main... end...." ); }}Copy the code

The finally clause is always executed

package com.wkcto.chapter02.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; /** * finally clause * The finally clause is always executed regardless of whether an exception is raised, Public class Test04 {public static void main(String[] args) {FileInputStream fis = null; Fis = new FileInputStream("d:/abc.txt"); System.out.println(" creates a channel between the current program and the specified file, "); fis.read(); // IOException is detected in the read() method; system.out.println (" read a byte from file ") is preprocessed. }catch (FileNotFoundException e) {e.printStackTrace(); // The generated exception is captured, and the program does not interrupt, }catch (IOException e) {// Catch (IOException e) {// FileNotFoundException inherits IOException. Println ("finally {system.out. println("finally {system.out. println("finally {system.out. println("finally {system.out. Always release system resources in the finally clause "); // You can close a channel in the finally clause if (fis! = null ) { try { fis.close(); } catch (IOException e) {e.printStackTrace(); } system.out.println (" Close the channel after reading the file "); } } System.out.println("main... end...." ); }}Copy the code
package com.wkcto.chapter02.demo01; /** * finally always executes * 1) Try /catch/finally clauses cannot be used alone. catch.. Try... Finally, you can also try... catch.. Public class Test05 {public static void main(String[] args) {int num = mm(10); System.out.println( num ); //10 } public static int mm( int xx ) { //xx = 10 try { return xx; } finally { xx++; } /* * the program executes until return xx; Finally, the value of xx increases by 1 to 11 *. Finally, the value of xx increases by 1 to 11 *. Finally, the value of xx increases by 1 to 11 *.Copy the code

Throws throw processing

package com.wkcto.chapter02.demo01; import java.io.FileInputStream; import java.io.FileNotFoundException; Public class Test06 {public class Test06 {public class Test06 {public class Test06 {public class Test06 { Interrupt routine, Public static void main(String[] args) throws FileNotFoundException {public static void main(String[] args) { System.out.println(" call m1() in main "); try { m1(); } catch (FileNotFoundException e) {e.printStackTrace(); // throw an exception thrown by m1(). } system.out. println("main method ends....") ); // If m1() is defined with an exception declared through throws, the exception should be preprocessed when m1() is called. Private static void m1() throws FileNotFoundException {system.out.println ("m1 method starts execution and calls m2() method "); m2(); System.out.println("m1 method ends......") ); Throws exception; throws exception; throws exception; throws exception; throws exception; throws exception; throws exception; throws exception; throws exception Exception declared with throws when m2() is defined is checked exception // Who calls m2(), Private static void m2() throws FileNotFoundException {system.out.println (" the m2 method calls the constructor of the FileInputStream class "); FileInputStream fis = new FileInputStream("d:/abc.txt"); System.out.println("m2 "); // The constructor needs to preprocess system.out.println ("m2 "). End "); }}Copy the code

Exception handling can be either caught or thrown. How to choose?

● When defining a method, if a statement in the method body has checked exceptions to be preprocessed, you can choose to capture processing, can also choose to throw processing.

● When calling a method, if the called method has checked exceptions that need to be preprocessed, the general situation is to capture processing.

● When defining a method, the exception thrown is thrown to the caller, by throwing an exception, to remind the caller of the method, there may be an exception, the caller needs to preprocess the exception

● When defining a method, if the method body throws an exception object through a throw statement, the method should declare the exception through throws

Above is “Introduction to Java Exception and Java Exception Handling” share, next for you to sort out “Java method override exception handling, Java exception in development applications and Java Array definition”.

Also welcome everybody exchange discussion, if this article has incorrect place, hope everybody many forgive.

Your support is my biggest motivation, if you help out, give me a thumbs up