CENTRE FOR DISTANCE AND ONLINE EDUCATION
CENTRE FOR DISTANCE AND ONLINE EDUCATION
LIVE SESSION – SESSION NO. 4
O02LA502– JAVA PROGRAMMING LAB.
MASTER OF COMPUTER APPLICATION
JOYASHRI BASAK
ASSISTANT PROFESSOR
At the end of this session, you should be able to:
Learning
• Perform Java coding using 2 D arrays for
Objectives Matrix Multiplication
• Perform string operations using various
methods of String class – equals(), charAt(),
length()
• Make use of superclass, subclass and extends
keyword.
Exercises in Java
1 Write a Java program to display employee details (employee name, salary) using class and method invocation
2 Write a java program to demonstrate the use of (i) increment (++) and decrement operator (--) (ii) IF-ELSE
Statement (iii) While loop.
3 Write a java program to perform matrix multiplication using 2-dimenstional array
4 Write a Java Program to check whether given String is Palindrome or not
5 Write a java program to demonstrate the use of super class and sub class concept.
6 Write a Java program using exception handling methods
7 Write a Java program to create a class and a subclass and demonstrate that a method can override.
8 Write a Java program to demonstrate the use of byte stream.
9. Write a Java program to create and start multiple threads that increment a shared counter variable
concurrently.
Exercise 6
Write a Java program using exception handling methods
Statement: Write a Java program to divide two integer numbers and catch the
exception, if divisor is 0.
• Java provides error handling by way of Exceptions.
• Exception is an unwanted or unexpected event, which occurs during the execution
of a program, i.e. at run time, that disrupts the normal flow of the program’s
instructions.
• Exceptions can be caught and handled by the program.
• Exception Handling is a mechanism to handle the runtime errors so that the normal
flow of the application can be maintained.
Exercise 6
• When an exception occurs within a method, it creates an object.
• This object is called the exception object which is thrown at runtime.
• It contains information about the exception, such as the name and description of
the exception and the state of the program when the exception occurred.
Example:
ClassNotFoundException, IOException, SQLException, RemoteException,
ArithmeticExceptions, ArrayIndexOutOfBoundException etc.
Exercise 6
Write a Java program using exception handling methods
Different approaches to handle exceptions in Java:
• try...catch block
• finally block
• throw and throws keyword
Exercise 6
Try block:
✓ Put Program statements that can raise exceptions with try block.
✓ If an exception occurs within the try block, it is thrown.
Catch block:
✓ catch the exception and handle it in some rational manner. System-generated
exceptions are automatically thrown by the Java run-time system.
✓ use the keyword throw to manually throw an exception.
✓ Any exception that is thrown out of a method must be specified as such by a throws
clause.
Finally block:
✓ Put any code that absolutely must be executed after a try block completes in a
finally block.
Exercise 6 : (Example code)
Statement: Write a Java program to divide two integer numbers and catch the
exception, if divisor is 0.
class Main { // Or execute following catch block, uncomment it.
public static void main (String[] args) { /*
catch (Exception e)
try { {
int x = 10; [Link](e);
int y = 0; [Link]("Value divided by zero is Undefined.");
int z; }
z = x/y; */
[Link](z); finally
} {
catch (ArithmeticException e) [Link]("finally block is always executed");
{ }
[Link]([Link]()); // rest of the program will be executed
[Link]("Value divided by [Link]("Outside try-catch-finally clause");
zero is Undefined."); }
} }
Key Learnings
▪
▪
▪
▪
▪
▪
STUDENT SUPPORT
Thank You !!