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
Question
Book Icon
Chapter 9.3, Problem 26STQ
Program Plan Intro

Exception:

  • It is nothing but an undesirable or unexpected event that occurs at run time which interrupts the normal flow of the program’s commands.
  • Exception handling is a mechanism that handle runtime errors like “ClassNotFoundException”, “SQLException”, and so on.
  • An advantage of exception handling is to maintain a flow of the program.
  • An exception can be handled using “try” and “catch” block.

Types of exceptions:

There are two types of exceptions. They are:

  • Checked Exception:
    • A class that inherits a “Throwable” class except “RunTimeException” and “Error” are called as checked exceptions.
    • Example: “IOException”
  • Unchecked Exception:
    • A class that inherits a “RunTimeException” class is called as unchecked exceptions.
    • Example: “ArrayIndexOutOfBoundException”

“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

}

Blurred answer
Students have asked these similar questions
Write a class TimeOfDay that uses the exception classes defined in the previous exercise. Give it a method setTimeTo(timeString) that changes the time if timeString corresponds to a valid time of day. If not, it should throw an exception of the appropriate type. [Java]
Consider the following test method code fragment, which tests whether a method given invalid input produces the expected exception in response. On which of the following lines should the test return false, indicating that the test has failed? There may be more than one correct answer! try { methodCall(invalidInput); // 1. line after method call } catch (IllegalArgumentException e) { // 2. correct } catch (Exception e) { // 3. incorrect } Group of answer choices 1. line after method call 2. correct 3. incorrect
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);    }}

Chapter 9 Solutions

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

Knowledge Booster
Background pattern image
Similar 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