Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 07:28:19 09月19日 Update

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.

Tags: Input user program information method divisor business logic situation divisor two code advantage disadvantage object more knowledge programmer article process Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Microsoft Xiaomi Apple