Throw Keyword
⚫ In Java, the throw keyword is used to explicitly throw
an exception from a method or block of code.
⚫ When an exception is thrown, the normal flow of
execution is interrupted, and the exception is
propagated up the call stack until it is caught by an
appropriate exception handler.
⚫ Syntax:
throw new ExceptionType("Error message");
Here, ExceptionType is the type of the exception being
thrown,
and the message is an optional string that provides more
information about the exception.
public class Example
{
public static void main(String[] args)
{
int denominator = 0;
try
{
if (denominator == 0)
{
throw new ArithmeticException("Cannot divide by zero");
}
int result = 100 / denominator;
System.out.println("Result: " + result);
}
catch (ArithmeticException e)
{
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Throws Keyword
⚫ In Java, the throws keyword is used to declare that a
method might throw one or more checked exceptions.
⚫ A checked exception is a type of exception that must
be caught or declared to be thrown by the method that
calls the method that might throw the exception.
Syntax:
public void method() throws ExceptionType1,
ExceptionType2 {
// method body
}
Here, ExceptionType1 and ExceptionType2 are the types
of the checked exceptions that might be thrown by the
method.
public class Example
{
public static void main(String[] args) throws
FileNotFoundException
{
File file = new File("example.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
}
User Defined Exceptions
⚫ In Java, you can create user-defined exceptions by
extending the Exception class or one of its subclasses.
⚫ To create a custom exception, you need to define a
new class that extends Exception or one of its
subclasses, such as RuntimeException.
⚫ You can then add any custom fields or methods that
are necessary for your exception.
public class CustomException extends Exception
{
public CustomException(String message)
{
super(message);
}
}
public class Example {
public static void main(String[] args)
{
try {
int number = -1;
if (number < 0)
{
throw new CustomException("Number must be positive");
}
System.out.println("Number is: " + number);
}
catch (CustomException e)
{
System.out.println("An error occurred: " + e.getMessage());
}
}
}
Multithreading
⚫ Thread:
A thread is a lightweight sub-process that runs
concurrently with other threads of the same program.
⚫ By using multiple threads, a program can perform multiple
tasks at the same time, which can improve performance
and responsiveness.
⚫ Threads allows a program to operate more efficiently by
doing multiple things at the same time.
⚫ Threads can be used to perform complicated tasks in the
background without interrupting the main program.
Life Cycle Of a thread(States)
1. New
2. Runnable
3. Running
4. Blocked
5. Dead
⚫ NEW
A thread that has not yet started is in this state.
⚫ RUNNABLE
A thread executing in the Java virtual machine is in
this state.
⚫ BLOCKED
A thread that is blocked waiting for a monitor lock is
in this state.
⚫ WAITING
A thread that is waiting indefinitely for another thread
to perform a particular action is in this state.
⚫ TIMED_WAITING
A thread that is waiting for another thread to perform
an action for up to a specified waiting time is in this
state.
⚫ TERMINATED
A thread that has exited is in this state.