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

What is the interpretation of Java exceptions and how to resolve them through business logic?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The purpose of this article is to share with you about the interpretation of Java exceptions and the ways to solve them through business logic. The editor thinks it is very practical, so I share it with you. I hope you can learn something after reading this article.

1. Exception architecture:

1.1: definition:

An abnormal situation that occurs during the operation of a program is called an exception.

The way 1.2:jvm solves the exception: 1. Terminate the running program

2. Output current exception information

A, the type of exception (an object that maintains the current exception internally in jdk uses the current object to describe the current exception)

B, the cause of the exception

C, the number of rows that may occur in the exception

1.3: an exception occurs in the program:

Package com.shsxt.exception

Import java.util.Scanner

/ *

* the user enters two numbers to find the quotient of two numbers

* java.util.InputMismatchException

*

*

, /

Public class Test02 {

Public static void main (String [] args) {

Scanner input = new Scanner (System.in)

System.out.println ("Please enter the divisor->")

Int num1 = input.nextInt ()

System.out.println ("Please enter divisor->")

/ / close the current Scanner

Input.close ()

Int num2 = input.nextInt ()

System.out.println (num1+ "/" + num2+ "=" + (num1/num2))

}

}

Error message:

Second, the way to solve exceptions through business logic:

Package com.shsxt.exception

Import java.util.Scanner

/ *

* manually resolve exception issues

*

* use a large number of if to determine the abnormal conditions that may occur during the execution of the program.

* advantage: it is true that abnormal information can be left to jvm to solve and display more friendly prompts.

* disadvantages: the amount of code written is too large and it is not easy to maintain at a later stage, and programmers never know what inputs the user has.

*

*

*

*

, /

Public class Test03 {

Public static void main (String [] args) {

Scanner input = new Scanner (System.in)

System.out.println ("Please enter the divisor->")

/ / determine whether the value entered by the user is what we need.

If (input.hasNextInt ()) {/ / whether the next value obtained is an int value if it returns true if it does not return fasle

Int num1 = input.nextInt ()

System.out.println ("Please enter divisor->")

/ / determine user input

If (input.hasNextInt ()) {

Int num2 = input.nextInt ()

If (num 2mm 0) {

System.out.println (num1+ "/" + num2+ "=" + (num1/num2))

} else {

System.out.println ("Divisor cannot be 0")

}

} else {

System.out.println ("user input divisor is incorrect")

}

} else {

System.out.println ("the divisor entered by the user is incorrect")

}

}

}

PS: note:

Through a large number of if to determine the abnormal situation that may occur during the execution of the program.

* advantage: it is true that abnormal information can be left to jvm to solve and display more friendly prompts.

* disadvantages: the amount of code written is too large and it is not easy to maintain at a later stage, and programmers never know what inputs the user has.

These are the ways to interpret Java exceptions and solve them through business logic. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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: 0

*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