Java Notes Unit IV
Java Notes Unit IV
Java Notes Unit IV
Java Errors:- The error indicates a problem that mainly occurs due to the lack of
system resources and our application should not catch these types of problems. Some
of the examples of errors are system crash error and out of memory error. Errors
mostly occur at runtime that's they belong to an unchecked type.
Types of Errors:
Errors may broadly classified into two categories:
i) Compile-time errors
ii) Run -time errors
i) Compile-time errors:
All syntax errors will be detected and displayed by the java compiler and therefore
these errors are known as compile-time errors. This compiler error indicates
something that must be fixed before the code can be compiled.
Most frequent Compile-Time errors are:
• Missing Parenthesis ( } )
• Printing the value of variable without declaring it
• Missing semicolon (terminator)
• Misspelling of identifiers and keywords
• Missing of double quotes in strings
ii) Run -time errors:
Sometimes, a program may compile successfully creating the .class file but not run
properly. Such programs may produce wrong results due to wrong logic and may
terminate due to errors such as stack overflow.
Most frequent Run -time errors are:
• Dividing an integer by zero
• Accessing an element that is out of the bounds of an array
• Passing a parameter that is not in a valid range or value for a method
• Converting invalid string to a number.
Exception:- An exception is an event that occurs during the execution of a program
that disrupts the normal flow of instructions during the execution of a program.
Exceptions are objects that represent errors that may occur in a Java program. An
exception is a condition that is caused by a run-time error in the program.
Exception Handling:-
syntax:
..........
..........
try
{
statement; // generates an exception
}
catch (Exception-type e)
{
statement; // processes the exception
}
..........
..........
Example:
class ExeceptionExample {
public static void main(String args[]) {
int a = 10;
int b = 5;
int c = 5;
int x, y;
try {
x = a / (b - c); // generates an exception
} catch (ArithmeticException e) {
System.out.println("Division by zero"); // processes the exception
}
y = a / (b + c);
System.out.println("y = " + y);
}
}
Syntax 2:
try
{
statement; // generates an exception
}
catch( Exception-type e)
{
statement; // processes the exception
}
catch( Exception-type e)
{
statement; // processes the exception
}
finally
{
statement; // final statement
}
Example:
class FinallyExample {
public static void main(String args[]) {
try {
int num = 25 / 5;
System.out.println(num);
} catch (ArithmeticException e) {
System.out.println(e);
} finally { // finally used after catch block
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}
// throw an exception
throw new ArithmeticException("Trying to divide by 0");
}
Multithreading:-
Multithreading in Java is a process of executing multiple threads simultaneously.
ava uses threads by using a "Thread Class". A single thread is basically a
lightweight and the smallest unit of processing. Thread class provides constructors
and methods to create and perform operations on a thread.
Creating Threads
Creating threads in java is simple. There are two ways to create a thread:
i) By extending Thread class
ii) By implementing Runnable interface.
Blocking a Thread:
A thread can also be temporarily suspended or blocked from entering into the
runnable and subsequently running state by using either of the following thread
methods:
sleep(t); // blocked for ‘t’ milliseconds
1. Newborn :
When we create a new Thread object using new operator, thread state is New
Thread. At this point, thread is not alive and it’s a state internal to Java
programming.
2. Runnable:
When we call the start() function on the Thread object, its state is changed to
Runnable. The control is given to the Thread scheduler to finish its execution.
Whether to run this thread instantly or keep it in a runnable thread pool before
running, depends on the OS implementation of the thread scheduler.
3. Running:
When a thread is executing, its state is changed to Running. Thread scheduler picks
one of the threads from the runnable thread pool and changes its state to Running.
Then CPU starts executing this thread. A thread can change state to Runnable,
Dead, or Blocked from running state depends on time slicing, thread completion of
the run() method, or waiting for some resources.
4. Blocked:
A thread can be waiting for other threads to finish using thread join or it can be
waiting for some resources to available. Once the thread wait state is over, its state
is changed to Runnable and it’s moved back to the runnable thread pool.
5. Dead:
Once the thread finished executing, its state is changed to Dead and it’s considered
to be not alive. A thread is in the terminated or dead state when its run() method
exits.