Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of try-catch structure of java exception handling

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of the try-catch structure of java exception handling, which is very detailed and has a certain reference value. Interested friends must read it!

Receive two integers from the keyboard and output the quotient of two numbers:

Package java_exception;import java.util.Scanner;public class TryDemoOne {public static void main (String [] args) {/ / defines two integers, enter two integers on the keyboard and output the quotient of two numbers Scanner input = new Scanner (System.in); System.out.println ("= operation starts ="); System.out.print ("Please enter the first integer:"); int one = input.nextInt () System.out.print ("Please enter the second integer:"); int two = input.nextInt (); System.out.println ("quotient of one and two:" + (one/two)); System.out.println ("= end of operation =");}}

If you use 0 or the letter as a divisor at this time, the program is bound to report an error.

In order to deal with this kind of error, try-catch can be introduced to prevent the program from reporting an error and stop running.

Introduce the try-catch structure where an exception may occur. When an error occurs in a certain sentence of the program, it will output the error message set by yourself in the catch (the program has an error! ), in which the e.printStackTrace (); statement will print the details of the error, and the position printed at the terminal is random. If there is no statement, it will only output the error report set by itself, and the program will run normally. :

Try {System.out.print ("Please enter the first integer:"); int one = input.nextInt (); System.out.print ("Please enter the second integer:"); int two = input.nextInt (); System.out.println ("quotient of one and two:" + (one/two));} catch (Exception e) {System.out.println ("Program error!") ; e.printStackTrace (); / / print the details of the error at random at the location of the terminal.

Output:

1. Enter two integers

= start of operation =

Please enter the first integer: 15

Please enter the second integer: 5

The quotient of one and two is: 3

= end of operation =

two。 Enter the letter an as a divisor

= Operation begins = Please enter the first integer: 15 Please enter the second integer: s program has an error! Java.util.InputMismatchException at java.base/java.util.Scanner.throwFor (Scanner.java:939) at java.base/java.util.Scanner.next (Scanner.java:1594) at java.base/java.util.Scanner.nextInt (Scanner.java:2258) at java.base/java.util.Scanner.nextInt (Scanner.java:2212) at java_exception.TryDemoOne.main (TryDemoOne.java:21) = end of operation =

3. Enter 0 as a divisor

= Operation begins = Please enter the first integer: 15 Please enter the second integer: 0 the program has an error! Java.lang.ArithmeticException: / by zero at java_exception.TryDemoOne.main (TryDemoOne.java:22) = end of operation =

If you want to ensure normal output after the statements contained in catch, you can add finally:

Try {System.out.print ("Please enter the first integer:"); int one = input.nextInt (); System.out.print ("Please enter the second integer:"); int two = input.nextInt (); System.out.println ("quotient of one and two:" + (one/two));} catch (Exception e) {System.out.println ("Program error!") ; e.printStackTrace (); / / print the error details at random} finally {System.out.println ("= end of operation =") at the terminal;} above are all the contents of the article "sample Analysis of try-catch structure for java exception handling". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 216

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report