Java programming exception simple code example
Exercise 1
Write a method void triangle (int a, int b, int c) to judge whether the three parameters can form a triangle. If not, an exception illegalargumentexception will be thrown to display the exception information: A, B, C "cannot form a triangle"; If it can be formed, the three sides of the triangle are displayed. Get three integers input from the command line in the main method, call this method, and catch exceptions.
The sum of the two sides is greater than the third side: a + b > C, and the difference between the two sides is less than the third side: c-a
Exercise 2:
Input 5 integers from the command line, put them into an integer array, and then print out. requirement:
If the input data is not an integer, to catch the input mismatch exception, display "please enter an integer"; If there are more than 5 input data, the array out of bounds exception will be caught, and "please enter 5 integers" will be displayed.
No matter whether an exception occurs or not, it outputs "thank you for using this program!"