Correct the code exercises:
1. Add the missing code and generate the correct output.
________{
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} _________ (Exception e) {
System.out.println("Something went wrong.");
}
2. Add the missing code to the
public class MyClass {
public static void main(String[] args) {
_______
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} catch (________) {
System._________._______ ("_________.");
}
}
}
Output: Something went wrong.
3. Add the missing code to the program in order to generate the given output.
public class MyClass {
public static void main(String[] args) {
_________{
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]);
} __________{
System.out.println("____________");
} ________{
System.out.println("_____________”);
}
}
}
Output:
Something went wrong.
The 'try catch' is finished.
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- Challenge 1: Division.java You have to implement a Division class that divides two numbers given by the user. The program should handle exceptions that could be thrown during operation: If the second number is 0 If any of the numbers is not a valid number, such as a letter.arrow_forwardالسؤال 1 .Write a program in java to enter length and width to find the area of a rectangle Use exception handling, if a user enters some wrong input, then program should throw an exception and this program should catch the exception example if in place of length or width a user enters some string value .then the program should throw an exception .Use finally keyword to print the statement استعراض الملفات المحلية إرفاق ملفarrow_forwardWhat is the value of the expression max().) O None It raises a ValueError exception. O It raises an IndexError exception.arrow_forward
- # Split input into 2 parts: name and age parts = input().split() name = parts[0] while name != '-1': # Insert try/except blocks to catch the exception. try: age = int(parts[1]) + 1 # printing name and age print('{} {}'.format(name, age)) except ValueError: # printing name and age as 0 print('{} {}'.format(name, 0)) # Get next line parts = input().split() name = parts[0]arrow_forwardThis programming assignment involves learning about some of the common exceptions that occur in Java programs. Consider the following exception types: NumberFormatException ArrayIndexOutOfBoundsException FileNotFoundException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception). Write 3 procedural programs and 1 Object Oriented Program. They can be very simple but make them up. You should write your code so that each exception type is forced to occur. Name your programs so that we can determine which exception you are using in the code. Example FileNotFoundException FileExists.java or something creative. Then, write a program that catches each type of thrown exception and display an error message indicating some of the details as to what that exception is. Write one program per type of exception. They can be…arrow_forwardWrite the code in java and please don't plagiarize or copy from other sources write it on your own. Read carefully and follow the instructions in the question. Thank you.arrow_forward
- X1155: Throwing an Exception The method below takes in a String parameter representing a username. It should throw an Exception if the username is less than 3 characters long. The method correctly throws the exception, but something is still missing. Fix the problem. Your Answer: Feedback 1 public String validateUser (String userName) 2|{ 3 45 6 7 8|} 9 if (userName.length() < 3) { throw new Exception("userName is too short"); } return userName; Check my answer! Reset Your feedback will appear here when you check your answer.arrow_forwardWrite the code in java and understand what the question says and give me the code and don't copy or plagiarize pleasearrow_forwardnew java code can only be added after line 17.arrow_forward
- The given program reads a list of single-word first names and ages (ending with-1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a string rather than an integer. At FIXME in the code, add try and except blocks to catch the ValueError exception and output 0 for the age. Ex: If the input is: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34arrow_forwardPlease written by computer source Implement try/except exception handler to catch all errors (from the following source code) separately: my_string = 'Hello World' print(my_string) num = int(my_string) print(my_string + 100) num = 1/0 print(total) print('Done') Submission Instructions: 1. Write all the code in one module (in one .py file), and save it as Firstname_Lastname_hw6.py (e.g., John Adam’s file name should be John_ Adam_hw6.py).arrow_forwardNeeded codearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education