I just need help on number 3. If it helps, the programming language that I am wo
ID: 3828370 • Letter: I
Question
I just need help on number 3. If it helps, the programming language that I am working with is JAVA.
Chapter 12 Exception Handling and Text llo (although command line arguments are covered in chapter 7 of the book, they were not discussed in class until chapter 12) 1. What is the general format for a Try-Catch block? 2. Assume that amt1 and amt are integers which have some values in them. Write a try-catch block that will try to divide amtl by amt2 and put the result into a variable named quotient. If an ArithmeticException occurs, print a message saying "Invalid data" and set quotient to 0. 3. Assume that the main method has a try block that includes a statement which calls method 1. Inside method 1 is another try block that includes a statement which calls method 2. Inside method2 is another try block that includes a statement which calls method3. What happens if method3 throws an exception? What happens if method2 has a catch block which can handle the exception? What happens if it does not have a catch block which can handle the exception? How does this progress if none of the methods can handle the exception (HINT: see page 460) 4. What are command line arguments and how can the program access them? Command line arguments are program variables which are supplied to the program at run time and these can be accessed by the parameters of the main function. 5. rite a program that will accept a list of strings as command line arguments. It wil step through these strings, convert each one to an integer and add them to a total. Finally, it will print the total.Explanation / Answer
3)
First JVM checks is there any catch block to handle the raised exception in the method3() .If available then that catch block will handle the exception.
If method3( ) is not having any catch block,then that raised exception will be thrown out of the try block of method3() then that exception is handled over to the caller of method3() .here the caller of method3() is method2().Then the jvm will check, is there is any catch block provided in the method2() to handle the exception .
What happen if method2() is having catch block which can handle the Exception ?
If method2() is having catch block then that exception will be caught by that catch block.
What happen if method2() is not having catch block which can handle the Exception ?
If method2() is also not having catch block then method2() will also throws that exception out of the try block then that exception will be handled over to the caller of the method2().So the exception will be handled over to the method1().Now jvm will check whether it is having any catch block.If it is not having catch block then the Exception will handled over to the caller of the method1().So that Exception will be thrown out of the try block and handled over to the caller of method1().
How does this progress if none of the methods can handle the Exception?
That Exception will be handled over to the main() method.Then It will handeld over to the JVM.As it is having default exception handler.That Exception will he handled by the Default Exception handler of JVM.Then Default Exception handler prints the Exception message on the console window.
_________________Thank You