Java: An Introduction to Problem Solving and Programming (7th Edition)
Java: An Introduction to Problem Solving and Programming (7th Edition)
7th Edition
ISBN: 9780133766264
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 9.3, Problem 24STQ

Explanation of Solution

“throws” clause:

  • • This clause is used to declare the exception. This provides information to the programmer that the program might produce an exception so it is better to give the exception handling code which maintains the normal flow of the program.
  • • It is usually given in the method header.

Syntax:

Method_Name throws ExceptionName

{

    //statements

}

Adding “throws” clause to the given method:

//throws clause is added to the Method header

public void ...

Blurred answer
Students have asked these similar questions
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.
Let'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);    }}
The following code is causing an exception during the runtime.  class test{ public static void main(String[] args) {    Object  o = "Hello";    Object s = new Random();    m(o);    m(s);     } public static void m(Object o) {       String s = (String)o;   System.out.println(s.length()); }} Exception can be prevented by adding an if statement and type casting in the method m Exception can be handled by adding  try-catch in the main method answers A or B both works Non of the answers

Chapter 9 Solutions

Java: An Introduction to Problem Solving and Programming (7th Edition)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education