Assignment On Java
Assignment On Java
Submitted By : Submitted To :
Answer:
Exception handling is one of the most important feature of java programming that allows us to
handle the runtime errors caused by exceptions.
First of all, we know about exception:
An Exception is an unwanted event that interrupts the normal flow of the program. When an
exception occurs program, execution gets terminated. In such cases we get a system generated
error message. The good thing about exceptions is that they can be handled in Java. By handling
the exceptions, we can provide a meaningful message to the user about the issue rather than a
system generated message, which may not be understandable to a user.
But The question arises that why an exception occurs?
There can be several reasons that can cause a program to throw exception. For example: Opening
a non-existing file in your program, Network connection problem, bad input data provided by
user etc.
Exception Handling:
If an exception occurs, which has not been handled by programmer then program execution gets
terminated and a system generated error message is shown to the user. For example, look at the
system generated exception below:
An exception generated by the system is given below
Java Servlets:
Java Servlets are software component that runs on a web server or an application server and be
responsible to receive the request from the web server, process the request, and respond back to
the server.
Servlets extends the capabilities of a server as they can respond to many types of requests, they
act like web containers for hosting web applications on web servers. They support nearly all the
client–server protocols, but is often used with HTTP and also known as HTTP servlet.
Architecture:
• The server has the web container containing servlet, which gathers data from the database
and creates a response.
• The response created by servlet is sent through HTTP Response to the client browser. The
question arises that how is the servlet’s response converted to HTTP Response format? As
the web server works purely on the HTTP protocol. Hence, this conversion from servlet’s
java.sql:
We have relational databases, from which at many times we need to access the data. For various
data processing related matters from RDDBMS we have java.sql package. The various classes in
the package are shown below:
Class Description
java socket:
A socket is one end-point of a two-way communication link between two programs running on
the network.
Socket class:
A socket is simply an endpoint for communications between the machines. The Socket class can
be used to create a socket.
Important methods
ServerSocket class:
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.
Important methods:
Example of Java Socket Programming:
Answer:
Thread: A thread, in the context of Java, is the path followed when executing a program. All
Java programs have at least one thread, known as the main thread, which is created by the Java
Virtual Machine (JVM).
Multi-thread:
Synchronization in java is the capability to control the access of multiple threads to any shared
resource. Java Synchronization is better option where we want to allow only one thread to access
the shared resource.
Deadlock:
Deadlock describes a situation where two or more threads are blocked forever, waiting for each
other. Deadlock occurs when multiple threads need the same locks but obtain them in different
order.
File handling:
File handling refers to working with the file in java. Reading files & writing into java files is
known as file handling in java. The FIle is a container that can contain different types of
information. The file can contain text, images, videos, tables, etc.
In Java, File class enables us to work with different types of files. File class is a member of the
java.io packages. Java provides different methods to read, write, update & delete files.
• In the below-given syntax, package java.io is imported into the program to work with the
file. java.io package exposes a File class that can be initiated by referencing the file to the
constructor of File class.
Some of the methods are given below for performing different operations in java:
Output:
7. Short note: Exception handling, Try catch block.
Answer:
Exception handling:
In Java, we use the exception handler components try, catch and finally blocks to handle
exceptions.
To catch and handle an exception, we place the try...catch...finally block around the code that
might generate an exception. The finally block is optional.
The syntax for try...catch...finally is:
The code that might generate an exception is placed in the try block.
Every try block should be immediately followed by the catch or finally block. When an
exception occurs, it is caught by the catch block that immediately follows it.
catch blocks cannot be used alone and must always be preceded by a try block.
Example 1: try...catch blocks
Output: