Question

In: Computer Science

Java try and catch problem public void method1(){ int[] array = new int[1]; try{ array[2] =...

Java try and catch problem

public void method1(){
int[] array = new int[1];

try{ array[2] = 0;}

catch(ArithmeticException e){array[2] = 0} // ?

finally{ return 0;}}

Could you please tell me why the line with the question mark will not report an error?

Solutions

Expert Solution

Detailed explanation :-

try{

//The code that might generate an exception is placed here .

// In the given code , array[2]=0 generates the exception . It is because the we try to store value 0 in a unallocated memory.

// When such exception occurs, it is caught by the catch block that immediately follows it.

}

catch{

//catch block handles such exceptions .

// Catch block is executed only when their is an exception in try block.Otherwise , catch block is skipped .

// catch block is used to handle the Exception by declaring the type of exception within the parameter.

// In the above code , ArithmeticException class is mentioned in the parameter .Hence , all the arithemtic Exceptions like array overflow are handled .Therefore , the error array[2]=0 is handled by the catch block and the code executes without any errors and the rest of program continues .

}

finally{

// finally block is always executed whether exception is found or not Java finally block follows try or catch block.

}

------------------------------------------------------------

Thank you.


Related Solutions

JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
What is Try and Catch in Java with example?
What is Try and Catch in Java with example?
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
Write a try/catch/finally clause to catch one or more specific Exception objects in Java.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT