“This is the 14th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: Yesterday we looked at some of the exception handling mechanisms, let’s continue to look at other forms of handling exceptions in Java, Java exception handling mechanism!

๐Ÿ˜ฎ Exception handling mechanism (key)

In Java applications, the exception handling mechanism is: throw an exception, catch an exception.

๐Ÿ‰ Catch exceptions try, finally, and catch

Try-catch, try-finally, try-catch-finally

Note: Catch statements can have one, more, or none. Finally, at most, must have a try.

So here you’re asking if there’s a separate try module, so I want to ask you, try is used to listen for exceptions, and if an exception occurs, who catches it? So no try appears alone. And the compilation doesn’t pass.

So like the try module, catch, finally, for example, cannot be used alone

  • The program usually does not report errors before running, but some unknown errors may occur after running. If you do not want to terminate the program due to exceptions, or if you do not want to throw it directly to the next level, you need to passtry-catchAnd other forms of exception capture, and then according to different exceptions to the corresponding processing.
  • Catch exceptions: In Java, exceptions are captured in specific statements that can be handled in a specified way.

Syntax for catching exceptions is as follows:

๐Ÿ”ด Today we’ll start with try-catch:

Catch (exception type E){handle exception code // log/print exception information/continue to throw exceptions}Copy the code

Such as:

public class TryCatchDemo { public static void main(String[] args) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Try {// When an exception is raised, it must be handled. Either capture or declare. Date date = simpleDateFormat.parse("2020-10-06"); } catch (ParseException e) {// what do I need to define in parentheses? // Define the exception type in parentheses e.prinintStackTrace (); } /* Public Date parse(String source) throws ParseException{} // Parse throws ParseException exception public class ParseException extends Exception {} */ } }Copy the code

How to obtain exception information:

The Throwable class defines some viewing methods:

  • public String getMessage(): Obtains the description of the exception and the cause (The cause is displayed when the information is displayed to the user.
  • public String toString(): Obtains the type and description of the exception.
  • public void printStackTrace(): Prints exception trace stack information and prints it to the console.
  • Specifically, we can look at:
public class TryCathDemo2 { public static void main(String[] args) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Try {// When an exception is raised, it must be handled. Either capture or declare. // Get the exception message. Date Date = simpleDateformat.parse ("2020ๅนด10ๆœˆ06"); } Catch (ParseException e) {//public String getMessage(): getMessage() System.out.println(e.getMessage()); //Unparseable date: "2020ๅนด10ๆœˆ06" system.out.println (e.tostring ()); / / Java. Text. A ParseException: Unparseable date: "in October 2020, 06" e.p rintStackTrace (); // Output information and float red!! /* java.text.ParseException: Unparseable date: "In October 2020, 06" at Java. Text. The DateFormat, parse (DateFormat. Java: 366) at com. It. Test3. TryCathDemo2. Main (TryCathDemo2. Java: 13) * /}}}Copy the code

What if multiple exceptions are caught?

  1. Multiple exceptions are handled separately.
  2. Multiple exceptions are captured and processed multiple times.

Generally, we use a capture multiple processing mode, the format is as follows:

Catch (exception type A e){When an exception of type A occurs in A try, the catch is used. Exception code // log/print exception information/continue to throw exceptions}catch(exception type B e){When an exception of type B occurs in a try, the catch is used. Exception handling code // log/print exception information/continue to throw exceptions}Copy the code

Such as:

public class TryCatchDemo3 { public static void main(String[] args) { //test(); test2(); } // Multiple exceptions are caught once and processed multiple times. public static void test2(){ int[] arr = {11, 22, 66, 0}; try { //System.out.println(arr[5]); // If this error occurs, the following code will not execute System.out.println(arr[2]); System.out.println(arr[0] / arr[arr.length-1]); } catch (ArithmeticException e) {system.out.println (" divisor is not 0"); } the catch (ArrayIndexOutOfBoundsException e) {System. Out. Println (" array subscript crossing the line "); }catch (Exception e) { e.printStackTrace(); Public static void test() {int a = 10; int b = 0; try { System.out.println(a / b); } catch (ArithmeticException e) {system.out.println (" divisor is not 0"); } int[] arr = {1, 2, 3}; try { System.out.println(arr[4]); } the catch (ArrayIndexOutOfBoundsException e) {System. Out. Println (" array subscript crossing the line "); // Array index out of bounds}}}Copy the code

Note: The method of handling the exceptions caught once and processed multiple times requires that the exceptions in multiple catches be different. In addition, if there is a relationship between the exceptions in multiple catches, the child exceptions should be handled in the catch above, and the parent exceptions should be handled in the catch below.

Such as:

๐ŸŒธ summary

If you are familiar with the concept of the exception handling mechanism, we will continue to talk about other forms of try tomorrow. Look forward to more in the next chapter. Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!

\