Concept explainers
Explanation of Solution
Given
//Main Class
class Question30
{
    //Main method
    public static void main(String[] args)
    {
        //Create an object for the class
        Question30 object = new Question30();
        try
        {
            //Print the statement
            System.out.println("Trying");
            //Call the method
            object.sampleMethod();
            //Print the statement
System.out.println("Trying after call.");
        }
        //Catch the exception
        catch(Exception e)
        {
            //Print the statement
            System...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: Introduction to Problem Solving and Programming
- Consider the following segment of JAVA code:void myFunction ( ) { int a = new int[15];boolean b =new boolean[5];System.arraycopy(a, 2, b, 2, 4);}What kind of exceptions occur in the above program? Justify your answer andrewrite the program to handle such exceptions.arrow_forwardWhat will be the output of the following Java program?    class exception_handling   {     public static void main(String args[])     {       try       {         int a[] = {1, 2,3 , 4, 5};         for (int i = 0; i < 7; ++i)           System.out.print(a[i]);       }       catch(ArrayIndexOutOfBoundsException e)       {      System.out.print("0");           }     }   } i) 12345 ii) 123450 iii) 1234500 iv) Compilation Errorarrow_forwardI. Output of public class Test implements Runnable { public void run() { System.out.println( "running!" ); } public void start( int i ) { } public static void main( String[] args ) { Test t = new Test(); Thread th = new Thread( t ); t.start( 1 ); } } II. Output of public static void main(String[] args) { int[] arr = { 0, 1, 2, 3, 4 }; System.out.println( arr[arr[arr.length-1]] ); } III. System.out.println (“3 in binary Nos=” + “Integer.toBinaryString(3)”); IV. Will the following java code compile? if not why? if yes write the output. abstract class A { abstract void callme(); // concrete methods are still allowed in abstract classes void callmetoo() { System.out.println("This is a concrete method."); } } class B extends A { void callmetoo() { System.out.println("B's implementation of callme."); } } class AbstractDemo { public static void main(String args[]) { B b = new B(); b.callmetoo(); } } } V.…arrow_forward
- 4.) How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j <= 5; j++) System.out.println(i * j) a. 100 b. 60 c. 50 d. 45 5.) Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; } a.Yes  b. No 6.) What exception type does the following program throw? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } } a. ArithmeticException b. ArrayIndexOutOfBoundsException c. StringIndexOutOfBoundsException d. ClassCastException e. No exceptionarrow_forwardpublic class X {    public static void main(String [] args)   {    try     {      badMethod();        System.out.print("A");      }     catch (RuntimeException ex) /* Line 10 */    {       System.out.print("B");     }     catch (Exception ex1)     {       System.out.print("C");     }     finally     {      System.out.print("D");     }     System.out.print("E");   }   public static void badMethod()   {     throw new RuntimeException();   } } Complete and explain.arrow_forwardEXPLAIN What is displayed when running the following program? Give reson why you got that answer.public class Test { public static void main(String[] args) {try {method();System.out.println("After the method call"); } catch (RuntimeException ex) {System.out.println("RuntimeException in main"); } catch (Exception ex) {System.out.println("Exception in main"); } }static void method() throws Exception {try {String s ="abc";System.out.println(s.charAt(3));}catch (RuntimeException ex) {System.out.println("RuntimeException in method()"); } catch (Exception ex) {System.out.println("Exception in method()"); }}}arrow_forward
- java Is there anything wrong with the following codes? If any fix them all.a.public class UnSafeClass {public static void unsafeMethod() throws Exception,RuntimeException {System.out.println("This is an unsafe method!");}public static void main(String[] args) {try {unsafeMethod();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} catch (RuntimeException e) {e.printStackTrace();}}}b.public class Nobody {public static void main(String[] args) {Somebody person = new Somebody() {String name = "nobody";public void printName() {System.out.println("I am " + name);}public void printName(String name) {System.out.println("I am " + name);}};person.printName("nobody");}}interface Somebody {public void printName();}c.public class SafeClass {public static double sqrt(double number) throws Exception {if (number < 0) throw new Exception("Negative number");else return Math.sqrt(number);}public static void main(String[] args) {System.out.println(sqrt(3));}}arrow_forwardLet's revisit chapter 5 example, but this time, no method can throw any exception out of the method and if an exception was detected then you will have to throw your own custom exception to let the user know what happened.  public class Chapter11Demo{ public static void main(java.lang.String[] args) {method1();}private static void method1(){ method2();}private static void method2(){ method3();}private static void method3(){ method4();}private static void method4(){ method5();}private static void method5(){ method6();}private static void method6(){    java.io.File in=new java.io.File("somefile.txt");    java.io.File out=new java.io.File("somefile2.txt");    java.util.Scanner inFile=new java.util.Scanner(in);    java.io.FileWriter outFile=new java.io.FileWriter(out);  }}arrow_forwardI have to create a Java program that lets the user interact with it, so they can learn about exceptions. How do I let the user interact with a list of options and direct them to a catch statement? Here is my program so far: import java.util.Scanner;import java.io.*;import java.util.InputMismatchException; public class Exceptions{         public static void main(String[] args)throws IOException{         // declare variables   char option = 'y';       //Arithhmetic & Array TEST   int array[] = {10,20,30,40};   int num1 = 50;   int num2 = 10;          // Setting up scanner to get user data   Scanner exc = new Scanner(System.in);       // Main method (menu, try-catch block)     // all within a loop     try {      }   // I want to learn how an ArrayIndexOutOfBoundsException...     catch(ArrayIndexOutOfBoundsException exception) {      System.out.println("This exception is thrown when the array index that was entered is…arrow_forward
- Write a program that illustrates rethrowing an exception. Define methods someMethod and someMethod2. Method someMethod2 should initially throw an exception. Method someMethod should call someMethod2, catch the exception and rethrow it. Call someMethod from method main, and catch the rethrown exception. Print the stack trace of this exception.arrow_forward-------------HW1_P1_INCOMPLETE.JAVA,,,,,,,,,,,,,,,,,,,,,,,,,, public class Hw1_part1 { public static void stats(int[] a) {// complete this method}public static void subarray(int[] a, int from, int to) {// error check w/o using Java's exception handlingif (from < 0 || to >= a.length) {System.out.println("Index out of bound");return;}// complete this method}public static void main(String[] args) {// test int[] a = {15, 25, 10, 65, 30, 55, 65};System.out.println("\nGiven array is: " + Arrays.toString(a));stats(a);subarray(a, 1, 4);// test with other arrays} }arrow_forwardHello, need help with this exercise in JAVA. We need to use the STATE DESIGN PATTERN.  GIVEN CODE: public class Pizza { public final static int COOKED = 0; public final static int BAKED = 1; public final static int DELIVERED = 2; int state = COOKED; public int getState() {return state; } public void setState(int state) {this.state = state; } public void bake() throws Exception { if(state == COOKED) { System.out.print("Baking the pizza..."); state = BAKED; } else if(state == BAKED) { throw new Exception("Can't bake a pizza already baked"); } else if(state == DELIVERED) { throw new Exception("Can't bake a pizza already delivered");} } public void deliver() throws Exception { if(state == COOKED) { throw new Exception("Can't deliver a pizza not baked yet"); } else if(state == BAKED) { System.out.print("Delivering the pizza..."); state = DELIVERED; } else if(state == DELIVERED) { throw new Exception("Can't deliver a pizza already delivered"); } } }  EXERCISE: Refactor the pizza class…arrow_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