CSE 230 Homework 2 I/O. Exceptions, Arrays, and Objects (13 Points) 1. 4 points)
ID: 3822061 • Letter: C
Question
CSE 230 Homework 2 I/O. Exceptions, Arrays, and Objects (13 Points) 1. 4 points) Briefly describe the difference between a Java Error and an Exception. Be sure to indicate whether the programmers is responsible for dealing with them. 2. (2 points) What is special about "runtime exceptions Give an example of one. 3. (2 points) There are two general phases to handling exceptions. What are they? 4. (2 points) The throws keyword is used to indicate two things. What are they? 5. (2 points) Briefly describe what happens when we throw within a method. 6. (3 points) In order to handle exceptions, we need try, catch, and, possibly, finally blocks. Explain briefly what each of these does. 7. (2 points) What code should appear in a try block? 8. (2 points) Why might we have multiple catch blocks after a single try? 9. (1 point) why might we want to re-throw exceptions, rather than processing them ourselves? 10. point) What happens if all methods just re-throw exceptions rather than handling them? 11. point) Describe the general purpose of a scanner. 12. (2 points) Consider the following code snippet: Scanner console In nev Scanner (System.in) String desc: consoleln.nextInt() desc consoleln.nextLine0 System out-println ("Count: count Description desc) What is printed if the program receives the following input? 10 13. (2 points) What would we have to do to the code in the previous question to make it print "Count: 10 Description Pencils when given the above input (Do not hard code that output. Use data read from the console.) Why does your change fix the problem? 4. point) What happens if we ask a Scanner for a double but it cannot couvert the next input ito a 15. (2 points) What do we need to do to create a Scanner that will read from a text file? 16. (1 point) Why do we need a try/catch block when reading from a file? 17. (3 points) What to we need to do to write to a file? 18. (1 point) What is the last step of either reading from or writing to a file. 19. point) Arrays of primitive types can be created using one line or two. Convert the following declara- ion to the two-line formatExplanation / Answer
1. Errors are something that a program can't do anything about. An exception is one, which can be handles by changing the coding style or by catching that and taking appropriate action while remaining in the program. Such as StackOverflowError and ArrayOutOfBoundException..
2. ArrayIndexOutOfBound is a runtime exception. RuntimeExceptions are exceptions that can be prevented programmatically. E.g. NullPointerException, ArrayIndexOutOfBoundException. If you check for null before calling any method, NullPointerException would never occur. They can be handles and catched.
3. The exeptions can be handled either using try-catch-finally block or by throwing the excepton from the method itself. These are the two mechanisms for handling.
4. The Java throws keyword is used to declare an exception. Also, it specifieds what kind of exception might be thrown out of the method. It gives an information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.
Please post only 4 sub questions inside one.. That is a guideline.. In order to explain you the things clearly.. You are told to provide less subparts in one question.. Filling a lot of question, makes it difficult for us to explain properly. Hope you got it!!. Thanks.