0% found this document useful (0 votes)
2 views12 pages

Java Prog Lab Ses4

The document outlines a live session for a Java Programming Lab, detailing learning objectives such as matrix multiplication using 2D arrays and string operations. It includes various exercises for students to practice Java coding, including exception handling, class inheritance, and multithreading. Key concepts of exception handling in Java are explained, along with example code for dividing two integers while catching exceptions.

Uploaded by

robosiddhibihar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Java Prog Lab Ses4

The document outlines a live session for a Java Programming Lab, detailing learning objectives such as matrix multiplication using 2D arrays and string operations. It includes various exercises for students to practice Java coding, including exception handling, class inheritance, and multithreading. Key concepts of exception handling in Java are explained, along with example code for dividing two integers while catching exceptions.

Uploaded by

robosiddhibihar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 !!

You might also like