Programming Fundamentals Lab Manual 13
Programming Fundamentals Lab Manual 13
Statement Purpose:
An exception is an object that signals the occurrence of an unusual event during the execution of a program. The process of creating this object that is generated an exception is called throwing an exception.
Activity Outcomes:
- Describe the notation of exception handling
- React correctly when certain exceptions occur
- Use Java’s exception-handling facilities effectively in classes and programs
1) Stage J (Journey)
Introduction
Unit testing can be done in two ways − manual testing and automated testing.
Manual Testing
- Executing a test cases manually without any tool support is known as manual testing.
- Time-consuming and tedious − Since test cases are executed by human resources, it is very slow and tedious.
- Huge investment in human resources − As test cases need to be executed manually, more testers are required in manual testing.
- Less reliable − Manual testing is less reliable, as it has to account for human errors.
- Non-programmable − No programming can be done to write sophisticated tests to fetch hidden information.
Automated Testing
- Taking tool support and executing the test cases by using an automation tool is known as automation testing.
- Fast − Automation runs test cases significantly faster than human resources.
- Less investment in human resources − Test cases are executed using automation tools, so less number of testers are required in automation testing.
- More reliable − Automation tests are precise and reliable.
- Programmable − Testers can program sophisticated tests to bring out hidden information.
A Unit Test Case is a part of code, which ensures that another part of code (method) works as expected. To achieve the desired results quickly, a test framework is required. JUnit is a perfect unit test framework for Java programming language.
A formal written unit test case is characterized by a known input and an expected output, which is worked out before the test is executed. The known input should test a precondition and the expected output should test a post-condition.
There must be at least two unit test cases for each requirement − one positive test and one negative test. If a requirement has sub-requirements, each sub-requirement must have at least two test cases as positive and negative.
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Write a java program that catches arithmetic exception.
Solution:
class Example1 {
public static void main(String args[]) {
int num1, num2; try {
// Try block to handle code that may cause exception num1 = 0;
num2 = 62 / num1;
System.out.println("Try block message");
} catch (ArithmeticException e) {
// This block is to catch divide-by-zero error
System.out.println("Error: Don't divide a number by zero"); }
System.out.println("After try-catch block in Java.");
}
}
Activity 2:
A java program for an array declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception.
Solution:
import java.io.*;
public class Example2{
public static void main(String args[]) { try {
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception thrown :" + e); }
System.out.println("Out of the block"); }}
Activity 3:
Java program to access the index in an array that is not present in it.
Solution:
public class Example3{
public static void main(String args[]) { int a[] = new int[2];
try {
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Exception thrown :" + e);
}finally { a[0] = 6;
System.out.println("First element value: " + a[0]);
System.out.println("The finally statement is executed");
}
}
}
Activity 4:
Java program for multiple try catch blocks.
Solution:
class Example4{
public static void main(String args[]){ try{
int a[]=new int[7];
a[4]=30/0;
System.out.println("First print statement in try block");
}
catch(ArithmeticException e){
System.out.println("Warning: ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Warning: ArrayIndexOutOfBoundsException");
}
catch(Exception e){
System.out.println("Warning: Some Other exception");
}
System.out.println("Out of try-catch block...");
}
}
Activity 5:
Java program for nested try loops
Solution:
class Example5{
public static void main(String[] args) { try {
int arr[]={5,0,1,2}; try {
int x=arr[3]/arr[1];
}
catch(ArithmeticException ae){
System.out.println("divide by zero");
}
arr[4]=3;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("array index out of bound exception"); } }}
3) Stage v (verify)
Home Activities:
Activity 1: Create your own exception sub class simply by extending java Exception class. You can define a constructor for your Exception sub class (not compulsory) and you can override the toString() function to display your customized message on catch.
4) Stage a2 (assess)
Assignment:
Write a statement that will throw an exception of type Exception if the value of the String variable named status is “bad”. The string recovered by get Message should be “Exception thrown: Bad Status.” You need not write the try block and catch block.
Comments
Post a Comment