Question

In: Computer Science

Whenever an exception arises, it terminates the program execution, which means it stops the execution of...

Whenever an exception arises, it terminates the program execution, which means it stops the execution of the current java program.
a) Identify and explain how the remedy for the exception is considered.
b) Implement the above-identified remedy in the java program to overcome the problem raised in the exceptional case when we try to open and close the files.

Solutions

Expert Solution

a)
The remedy for exception is to handle them.
In java we can handle them with try-catch block.
Using try-catch block handles the exception.
in the try block we write the required code.
In the catch block we write the appropriate message to display.
try block will have exception expected lines.
if there any exception raised then appropriate catch block will be executed.
b)
Opening and closing file exceptions will be in IOException class.
So we need to write the open and close file statements in try block and IOException is catched in catch block.
Here the filereader tries to open the file and then buffer readers takes the file opened.
Then we try to close the file by closing the bufferreader.
If there any exceptions the catch executes.

CODE OF IMPLEMENTATION OF FILE STREAMS:

iimport java.util.*;
import java.lang.*;
import java.io.*;
class fileExceptns
{
   public static void main(String[] args)
   {
       //try block where we write the opening and closing file lines of code.
       try
       {
           //we here try to open a txt file.
           FileReader reader = new FileReader("svsc.txt") ;
       BufferedReader in = new BufferedReader(reader) ;
       System.out.println("File is opened successfully");
       in.close();//here we try to close the file bufferreader.
       System.out.print("File is closed successfully");
       }
       //if there any input output exception in the try block then this
       //block executes and prints the exception.
       catch (IOException e)
       {
           //printing the IO exception.
           System.out.println("The exception is "+e);
       }
}
}

CODE ATTACHMENTS:

OUTPUTS:

The first output is there is no file then so the exception file notfound is preinted.

The second output i have created the file so it printed successful.

All IO exceptions are handled in this.


Please do comment for any queries.
PLease like it.
Thank You.


Related Solutions

Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This...
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.
1.       Test the execution time of the program in Code 3. Make a screen capture which...
1.       Test the execution time of the program in Code 3. Make a screen capture which shows the execution time that has the unit of machine time unit. Code3 SOURCE.CPP # include<fstream> # include "List.h" # include <iostream> using namespace std; int main() { List temps; int oneTemp; ifstream inData; ofstream outData; inData.open("temp.dat"); if (!inData) {   outData<<"Can't open file temp.dat" << endl;   return 1; } inData >> oneTemp; while (inData && !temps.IsFull()) {   if (!temps.IsPresent(oneTemp))    temps.Insert(oneTemp);   inData >> oneTemp; }...
Modify the below program so that the following two conditions are met: 1. Each child terminates...
Modify the below program so that the following two conditions are met: 1. Each child terminates abnormally after attempting to write to a location in the read-only text segment. 2. The parent prints output that is identical (except for the PIDs) to the following: child 12255 terminated by signal 11: Segmentation fault child 12254 terminated by signal 11: Segmentation fault Hint: Read the man page for psignal(3) ___________________________________. #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h>...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to be thrown when a   string is discovered that has too many characters in it. Consider a   program that   takes   in the last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,   throw   the   exception.  ...
1. write a program for the msp430fr6989 that will light LED1, whenever S1 is pressed. It...
1. write a program for the msp430fr6989 that will light LED1, whenever S1 is pressed. It should turn off whenever S1 is released. 2. Write a program that will make LED1 blink 5 times when S1 is pressed, and then stop.
Make a detailed trace of the execution of the following program fragment, giving the contents of...
Make a detailed trace of the execution of the following program fragment, giving the contents of all the registers, and memory locations involved. Assume that before execution begins the SS register contains 0410h, and the SP register 0100h, and that the contents of AX, BX, CX, and DX are 789Ah, 0020h, 2000h, and 1234h respectively. SS SP TOS AX BX CX DX Initial contents 0990 0100 / 789A 0020 2000 1234 after PUSH AX after PUSH BX after PUSH CX...
What is an EA implementation methodology? Who is responsible for execution of the EA program and...
What is an EA implementation methodology? Who is responsible for execution of the EA program and EA methodology? How often should an EA be updated? Why?
Which of the following best describes how ubiquitin terminates Cdk complex activity? Group of answer choices...
Which of the following best describes how ubiquitin terminates Cdk complex activity? Group of answer choices Ubiquitin promotes transcription of Cdk complex inhibitors Ubiquitin chains are added to the cyclin, directing it to the proteasome for degradation Ubiquitin activates membrane pumps that re-establish Ca2+ gradients Ubiquitin binds to the Cdk active site and inhibits its enzyme activity
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT