Java Unit-4
Java Unit-4
Multithreading in Java
1. What is thread, multithreading?
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing
and multithreading, both are used to achieve multitasking.
Multithreading in java is a process of executing multiple threads simultaneously.
However, we use multithreading than multiprocessing because threads use a shared memory
area. They don't allocate separate memory area so saves memory, and context-switching
between the threads takes less time than process. Java Multithreading is mostly used in
games, animation, etc.
Advantages of Java Multithreading
1) Enhanced performance.
2) You can perform many operations together, so it saves time. We can achieve multitasking.
3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single
thread.
4) Threads use a shared memory area. No need of Extra memory .
1)New: The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
2)Runnable: The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread.
3)Running: The thread is in running state if the thread scheduler has selected it.
4)Waiting (Blocked):This is the state when the thread is still alive, but is currently not eligible
to run.
5) Dead(Terminated) : A thread is in terminated or dead state when its run() method exits.
2
UNIT-4(OOPS Through Java)
Exception Handling
1. Explain types of error in java?
Compile time errors: Compile time errors refer to syntax and semantics in compile time. For
example, if you do operations that involves different types. Ex: adding a string with an int, or
dividing a string by a real. These are also refereed as Checked exceptions.
Run Time error: Run time errors are those that are detected when the program execute. For
example, division by zero. The compiler cannot know if the operation x/a-b will leads to division
by zero until the execution. These are also refereed Unchecked exceptions.
Errors: These are not exceptions at all, but problems that arise beyond the control of the user or
the programmer. Errors are typically ignored in your code because you can rarely do anything
about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored
at the time of compilation.
2. What is an exception and explain types ?
An exception (or exceptional event) is a problem that arises during the execution of a
program. When an Exception occurs the normal flow of the program is disrupted and the
program/Application terminates abnormally, which is not recommended, therefore, these
exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios.
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or the JVM has run
out of memory.
There are two types of exceptions in Java:
1)Checked exceptions 2)Unchecked exceptions
5
UNIT-4(OOPS Through Java)
Checked exceptions
All exceptions other than Runtime Exceptions are known as Checked exceptions as the
compiler checks them during compilation to see whether the programmer has handled them or
not. If these exceptions are not handled/declared in the program, you will get compilation error.
For example, SQLException, IOException, ClassNotFoundException etc.
import java.io.File;
import java.io.FileReader;
public class FilenotFound_Demo {
public static void main(String args[]) {
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
}
}
Output
C:\>javac FilenotFound_Demo.java
FilenotFound_Demo.java:8: error: unreported exception FileNotFoundException; must be caught
or declared to be thrown
FileReader fr = new FileReader(file);
^
1 error
Unchecked Exceptions
Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not
checked at compile-time so compiler does not check whether the programmer has handled them
or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe
exit.For example, ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc.
public class Unchecked_Demo {
public static void main(String args[]) {
int num[] = {1, 2, 3, 4};
System.out.println(num[5]);
}
}
Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)
Errors − These are not exceptions at all, but problems that arise beyond the control of the user
or the programmer. Errors are typically ignored in your code because you can rarely do anything
about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored
at the time of compilation.
6
UNIT-4(OOPS Through Java)
Keyword Description
Try The "try" keyword is used to specify a block where we should place exception code. The try
block must be followed by either catch or finally. It means, we can't use try block alone.
Catch The "catch" block is used to handle the exception. It must be preceded by try block which
means we can't use catch block alone. It can be followed by finally block later.
Finally The "finally" block is used to execute the important code of the program. It is executed
whether an exception is handled or not.
Throws The "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies
that there may occur an exception in the method. It is always used with method signature.
c=b/a;
System.out.println("This line will not be executed");
}
catch(ArithmeticException e)
{
System.out.println("Divided by zero");
}
System.out.println("After exception is handled");
}
}
Output: Divided by zero
After exception is handled
An exception will thrown by this program as we are trying to divide a number by zero
inside try block. The program control is transferred outside try block. Thus the line "This line
will not be executed" is never parsed by the compiler. The exception thrown is handled
in catch block. Once the exception is handled, the program control is continue with the next line
in the program i.e after catch block. Thus the line "After exception is handled" is printed.
{
System.out.println("array index out of bound exception");
}
}
}
Output: divide by zero
Note: At a time, only one exception is processed and only one respective catch block is
executed.
c) finally clause
A finally keyword is used to create a block of code that follows a try block. A finally
block of code is always executed whether an exception has occurred or not. Using a finally
block, it lets you run any cleanup type statements that you want to execute, no matter what
happens in the protected code. A finally block appears at the end of catch block.
Output:
Exception in thread main java.lang.ArithmeticException:not valid
10
UNIT-4(OOPS Through Java)
4) Throw is used within the method. Throws is used with the method
signature.