Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of Java exception handling Operation

Shulou Source: shulou.com Published: 2022-06-02 13:06:14 09月20日 Update

This article introduces the relevant knowledge of "Java exception handling operation case analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The use of exception handling-the ability to catch exceptions

1 code

Public class DealException {public static void main (String [] args) {try / / check the code of this block {int arr [] = new int [5]; arr [10] = 7; / / an exception will occur here} catch (ArrayIndexOutOfBoundsException e) {System.out.println ("Array out of bind!" );} finally / / the program code of this block must execute {System.out.println ("it must be executed here!" );} System.out.println ("main () method ends!" );}}

2 run

Array is out of bind range! This place will be executed! The main () method ends!

Second, the use of exception handling-the exception cannot be caught

1 code

Public class DealException {public static void main (String [] args) {try / / check the code of this block {int arr [] = new int [5]; arr [10] = 7; / / an exception will occur here} catch (ArithmeticException e) {System.out.println ("arithmetic exception");} finally / / the program code of this block must execute {System.out.println ("it must be executed here!" );} System.out.println ("main () method ends!" );}}

2 run

This place will be executed! Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at DealException.main (DealException.java:9)

3 description

For the uncaught exception scenario, the finally statement block will be executed, but the statement after the finally language block cannot be executed, so the exception will be handed over to JVM to handle, and finally stop the program from running.

The use of three exception objects ex

1 code

Public class excepObject {public static void main (String [] args) {try {int arr [] = new int [5]; arr [10] = 7;} catch (ArrayIndexOutOfBoundsException ex) {System.out.println ("the array is out of range!" ); System.out.println ("exception:" + ex); / / display the contents of the exception object e / / ex.printStackTrace ();} System.out.println ("main () method ends!" );}}

2 run

Array is out of bind range! Exception: end of java.lang.ArrayIndexOutOfBoundsException: 10main () method!

Fourth, catch exceptions through multiple catch

1 code

Public class arrayException {public static void main (String args []) {System.out.println ("- A, before calculation starts"); try {int arr [] = new int [5]; arr [0] = 3; arr [1] = 6; arr [1] = 0; / / Divisor 0, exception arr [10] = 7; / / Array subscript crosses the bounds with exception int result = arr [0] / arr [1] System.out.println ("- B, division result:" + result);} catch (ArithmeticException ex) {ex.printStackTrace ();} catch (ArrayIndexOutOfBoundsException ex) {ex.printStackTrace ();} finally {System.out.println ("- this will be executed regardless of whether there is an error or not!") ;} System.out.println ("- C, after the calculation is over.") ;}}

2 run

-A, java.lang.ArrayIndexOutOfBoundsException: 10 at arrayException.main (arrayException.java:19) before the calculation starts-regardless of whether there is an error or not, it will be executed here! -C. after the calculation is over.

This is the end of the content of "Java exception handling operation example analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Code processing array method program run scope content instance instance analysis analysis object more knowledge statement check practical learned next subscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux MariaDB NVidia macOS