Assignment 06_
Assignment 06_
DEPARTMENT OF MATHEMATICS
B.Sc. honours in financial mathematics and industrial statistics
MSF1123: PROGRAMMING TECHNIQUES
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.
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");
}
}
}
****************************************