0% found this document useful (0 votes)
5 views

Assignment 06_

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 06_

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNIVERSITY OF RUHUNA

DEPARTMENT OF MATHEMATICS
B.Sc. honours in financial mathematics and industrial statistics
MSF1123: PROGRAMMING TECHNIQUES

Assignment No:06 Semester I, 2023


Submit the answers on or before 10.00am on 24/11/2023.

1. Try to execute the following code.

class Main{
public static void main(String[] args) {
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}
}

Use a suitable exception handling strategy to prevent program termination and display
your own meaningful message to indicate the issue.

2. Check the output by executing the following java programs.

(a) import java.io.*;


public class ExcepTest {
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");
}
}

(b) class TestFinallyBlock {


public static void main(String args[]){
try{
int data=25/5;
System.out.println(data);
}
catch(NullPointerException e){
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
(c) public class TestFinallyBlock1{
public static void main(String args[]){
try {
System.out.println("Inside the try block");
int data=25/0;
System.out.println(data);
}
catch(NullPointerException e){
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}

(d) public class TestFinallyBlock2{


public static void main(String args[]){
try {
System.out.println("Inside try block");
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e){
System.out.println("Exception handled");
System.out.println(e);
}
finally {
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}

3. (a) Create a java program to throw an exception if age is below 18 print “Access denied”.
If age is 18 or older, print “Access granted”.
(b) Write the java program to get “age” as a user input.
4. Execute the following Java code with multiple catch blocks and get the intended output.

class Main {
public static void main (String[] args) {
try{
try{
int[] a={1,2,3,4,5};
System.out.println(a[5]);
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Out of bounds");
}
System.out.println(5/0);
}
catch(ArithmeticException e)
{ System.out.println("ArithmeticException : divide by 0");
}
}
}

5. Try to execute the following code. If there are any errors, correct them and check the
output.

class Main {
public static void main (String[] args) {
try{
System.out.println(4/0);
}catch(Exception e){
System.out.println("Exception : divide by 0");
}catch(ArithmeticException e){
System.out.println("ArithmeticException :divide by 0");
}
}
}

****************************************

You might also like