Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of exception handling in Java Development

Shulou Source: shulou.com Published: 2022-06-01 07:59:41 09月20日 Update

This article mainly introduces the example analysis of exception handling in the process of Java development, which is very detailed and has a certain reference value. Interested friends must read it!

1. An exception occurred while running java:

I am here because: arr [3] does not exist:

Java.lang.ArrayIndexOutOfBoundsException: 3

Public class btyf {public static void main (String [] args) {int [] arr= {1mt 2je 3}; System.out.println (arr [0]); System.out.println (arr [3]); System.out.println (arr [1]) / / 1 exception ArrayIndexOutOfBoundsException exception name / / btyf.main (btyf.java:13) exception location Line 13 / Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3Accord / at btyf.main (btyf.java:13)}}

Results:

Java virtual machine: will output abnormal content to the console

two。 Handling exceptions:

Public class btyf {public static void main (String [] args) {int [] arr= {1pr 2pr 3}; System.out.println (arr [0]); try {System.out.println (arr [3]);} catch (ArrayIndexOutOfBoundsException e) {System.out.println ("the array index you accessed does not exist"); e.printStackTrace () / / output exception data: console} System.out.println (arr [1]); / / 1 exception / / ArrayIndexOutOfBoundsException exception name / / btyf.main (btyf.java:13) exception location / / Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3pm / at btyf.main (btyf.java:13)}}

Results:

Catch exceptions through try, and the code without exceptions will not stop because of some exceptions in the previous code.

It can be executed.

3.throwable: member method:

System.out.println (e.toString ()); / / print out the exception: location and name

E.printStackTrace (); / / output exception data: console

System.out.println (e.getMessage ()); same

Multi-purpose: System.out.println (e.toString ()); this

Try {System.out.println (arr [3]);} catch (ArrayIndexOutOfBoundsException e) {/ / System.out.println ("the array index you accessed does not exist"); / / e.printStackTrace (); System.out.println (e.getMessage ()); / / public String getMessage () {/ / return detailMessage; / /} System.out.println (e.toString ());}

Results:

4.throws: throw an exception:

But in the exception: still add try catch

Add location: exception member method

Public static void main (String [] args) throws ArrayIndexOutOfBoundsException {}

Code:

Public class uytig {public static void main (String [] args) throws ArrayIndexOutOfBoundsException {int [] arr= {1Power2) 3}; System.out.println (arr [0]); try {System.out.println (arr [3]);} catch (Exception e) {e.printStackTrace ();} System.out.println ("in progress");}}

The above is all the contents of the article "sample Analysis of exception handling problems in Java Development". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Location content processing code console results control output examples procedures problems analysis development members data arrays methods articles indexes value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Shulou Information Xiaomi Apple