Write My Paper Button

WhatsApp Widget

Introductionto Programming

T121: ICT102 Introductionto Programming
Tutorial 11

Topic:
Submission:
Description:
Debugging and Exception Handling
A day after your tutorial Session.

AssignmentTutorOnline

This tutorial will introduce you to Exception Handling and Run Time Errors. You should be
able to understand difference between Syntax (compiler) and run time (Exceptions) errors.
Also, to handle run time errors for graceful exit of programs.
Exercise 1: Output of the program and Run Time Errors
1. What is printed by the following application?
public class Example1

public static void main(String[] args)

int denominator, numerator, ratio;
numerator = 5;
denominator = 2;
ratio = numerator / denominator;
System.out.println(“The answer is: “+ratio);
System.out.println(“Done.”); // Don’t move this line

2. Change the value of denominator to 0. What runtime error message is now generated
by this application?
3. Change the value of denominator to 0. What runtime error message is now generated
by this application?
4. Add a try-catch statement to this application. Specifically, put only the statement that
generated the exception inside of the try block and put no statements in the catch
block. (Hint: You should be able to determine what exception to catch from the error
message that you received during the previous step. You should also be able to
determine the line number of the statemen that generated the exception if you didn’t
change the white space.) What statements are now in the body of the main() method?
5. Modify the body of the main method as follows.
int denominator, numerator, ratio;
numerator = 5;
denominator = 0;
try

ratio = numerator / denominator;
System.out.println(“The answer is: “+ratio);

catch (ArithmeticException ae)

System.out.println(“Divide by 0.”);

System.out.println(“Done.”); // Don’t move this line
6. Will this version of the application compile? Why or why not?
Exercise 2: ArrayOutofBound
// A Common cause index out of bound
public class NewClass2

public static void main(String[] args)

int ar[] = 1, 2, 3, 4, 5;
for (int i=0; iSystem.out.println(ar[i]);

What is output and why this code generates run time exception? How to write a try and catch
block to handle this exception?
Submission Task: File not Found
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileIO

public static void main(String[] args)

String filename = “…documentsaFile.txt”;
BufferedReader br;
// try
//
br = new BufferedReader(new FileReader(filename));
String line = “”;
while((line = br.readLine()) != null)

//
System.out.println(“Reading line: ” + line);
/* catch (FileNotFoundException e)
//
System.out.println(“There was an exception! The file was not
found!”)
;

catch (IOException e)

System.out.println(“There was an exception handling the file!”);
*/

1. What is output of code?
2. Uncomment the try and catch block? Now what is shown in output?
3. please create an “aFile.txt” file on your “…documents” on C: drive, and put
the following content in it:
This is line 1 of the file
This is line 2
4. What is output and why?

Don`t copy text!
WeCreativez WhatsApp Support
Our customer support team is here to answer your questions. Ask us anything!
???? Hi, how can I help?