0% found this document useful (0 votes)
5 views5 pages

Java Exception Handling and Threads Guide

The document covers Java exception handling using try-catch blocks, including built-in exceptions like ArithmeticException and ArrayIndexOutOfBoundsException, as well as creating custom exceptions. It also discusses thread creation methods, multi-threading, and thread priority management in Java. Sample code snippets illustrate each concept, demonstrating practical applications of exception handling and threading.

Uploaded by

aditishirole2112
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)
5 views5 pages

Java Exception Handling and Threads Guide

The document covers Java exception handling using try-catch blocks, including built-in exceptions like ArithmeticException and ArrayIndexOutOfBoundsException, as well as creating custom exceptions. It also discusses thread creation methods, multi-threading, and thread priority management in Java. Sample code snippets illustrate each concept, demonstrating practical applications of exception handling and threading.

Uploaded by

aditishirole2112
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

Java Unit - 3

1.​ Try - Catch Block

package myfirstproject;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ int a= 10;
​ ​ int b =0;
​ ​ try
​ ​ {
​ ​ ​ int result = a / b;
​ ​ ​ [Link]("Result : "+result);
​ ​ }
​ ​ catch(ArithmeticException e)
​ ​ {
​ ​ ​ [Link]([Link]());
​ ​ }
​ ​ catch(Exception e)// We can Write Multiple Catch blocks to catch the exception proplerly
​ ​ {
​ ​ ​ [Link]([Link]());
​ ​ }
​ }
}

2.​ Built in exceptions :

●​ Arithmetic Exception - when we try to do wrong arithmetic operation

package myfirstproject;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ int a= 10;
​ ​ int b =0;
​ ​ try
​ ​ {
​ ​ ​ int result = a / b;
​ ​ ​ [Link]("Result : "+result);
​ ​ }
​ ​ catch(ArithmeticException e)
​ ​ {
​ ​ ​ [Link]([Link]());
​ ​ }
​ }
}

●​ ArrayIndexOutOfBoundsException - when we try to access element beyond


array size

package myfirstproject;
class practical
{
​ public static void main(String[] args)
​ {
​ ​ int[] a = {10,20,30,40,50};
​ ​ try
​ ​ {
​ ​ ​ [Link]("7th element of array : "+a[-1]);// throw exception because no
-1th index at array
​ ​ ​
​ ​ }
​ ​ catch(ArrayIndexOutOfBoundsException e)
​ ​ {
​ ​ ​ [Link]([Link]());
​ ​ }
​ }
}

3.​ Custom Exception - We can Create our own Exceptions

Ex : -
●​ Write a program that throws an exception called 'NoMatchException' when a string is not
equal to 'India'. (exp 12)

import [Link].*;
class NoMatchException extends Exception
{
​ NoMatchException(String s)
​ {
​ ​ super(s);
​ }
}
class practical
{
​ public static void main(String[] args)
​ {
​ ​ Scanner sc = new Scanner([Link]);
​ ​ [Link]("Enter String : ");
​ ​ String s = [Link]();
​ ​ try
​ ​ {
​ ​ ​ check(s);
​ ​ }
​ ​ catch(NoMatchException e)
​ ​ {
​ ​ ​ [Link]([Link]());
​ ​ }
​ }
​ public static void check(String s) throws NoMatchException
​ {
​ ​ if([Link]("India"))
​ ​ {
​ ​ ​ [Link]("Correct !");
​ ​ }
​ ​ else
​ ​ {
​ ​ ​ throw new NoMatchException("Incorrect ! not India");
​ ​ }
​ }
}

4.​ Thread

●​ There are two ways to create thread

1.​ By extending Thread class


​ ​
package myfirstproject;
class myThread extends Thread
{
​ public void run()
​ {
​ ​ [Link]("Thread is Running !");
​ }
}
class practical
{
​ public static void main(String[] args)
​ {
​ ​ myThread t = new myThread();
​ ​ [Link]();
​ }
}

2.​ By implementing Runnable interface

package myfirstproject;
class myThread implements Runnable
{
​ public void run()
​ {
​ ​ [Link]("Thread is Running !");
​ }
}
class practical
{
​ public static void main(String[] args)
​ {
​ ​ myThread t = new myThread();
​ ​ Thread t1 = new Thread(t);
​ ​ [Link]();
​ }
}

5. Multi Threading : in java, multiple threads can execute at the same time

Ex :
●​ Write a program to create two threads. One thread will display the odd numbers from 1
to 50 and the other thread will display the even numbers. (exp 11)

package myfirstproject;
class even extends Thread
{
​ public void run()
​ {
​ ​ for(int i = 1;i<=50;i++)
​ ​ {
​ ​ ​ if(i%2==0)
​ ​ ​ {
​ ​ ​ ​ [Link]("Even : "+i);
​ ​ ​ }
​ ​ }
​ }
}
class odd extends Thread
{
​ public void run()
​ {
​ ​ for(int i = 1;i<=50;i++)
​ ​ {
​ ​ ​ if(i%2!=0)
​ ​ ​ {
​ ​ ​ ​ [Link]("Odd : "+i);
​ ​ ​ }
​ ​ }
​ }
}
class practical
{
​ public static void main(String[] args)
​ {
​ ​ even e = new even();
​ ​ odd o = new odd();
​ ​ [Link]();
​ ​ [Link]();
​ }
}

6. Thread Priority - we can Assign priority to our thread , that means High priority task will
execute first ;

package myfirstproject;
class myThread extends Thread
{
​ public void run()
​ {
​ ​ [Link]("Thread is Running !");
​ }
}
class practical
{
​ public static void main(String[] args)
​ {
​ ​ myThread t = new myThread();
​ ​ // set priority to thread
​ ​
​ ​ [Link](Thread.MAX_PRIORITY);
​ ​ [Link]("Priority of thread : "+[Link]());
​ ​
​ ​ [Link](Thread.MIN_PRIORITY);
​ ​ [Link]("Priority of thread : "+[Link]());
​ ​
​ ​ [Link](Thread.NORM_PRIORITY);
​ ​ [Link]("Priority of thread : "+[Link]());
​ ​
​ }
}

Common questions

Powered by AI

Threads in Java can be created in two ways: by extending the Thread class or by implementing the Runnable interface. When a class extends Thread, it must override the run() method, and an instance of this class can be started with the start() method, which internally calls the run() method . On the other hand, implementing Runnable involves defining a class that implements the Runnable interface and providing an implementation for the run() method. An instance of this class is then passed to a Thread object, which is started using the start() method . The main conceptual difference lies in inheritance; extending Thread means the class cannot extend any other class, whereas implementing Runnable is more flexible as it allows the class to extend another class as well.

Using the Runnable interface is often recommended over extending the Thread class because it offers better design flexibility and allows for greater class hierarchy compatibility. A class implementing Runnable can extend another class, adhering to Java's single inheritance model, which extends Thread does not allow. Additionally, implementing Runnable is typically considered a better practice as it separates the task from the thread, promoting a cleaner separation of concerns . This design supports better code reusability and enables multiple threads to execute the same Runnable object, making it a versatile option for thread creation.

The use of multithreading to execute even and odd number sequences concurrently in a Java program allows for parallel processing, which can significantly enhance the program's efficiency and responsiveness. Each thread operates independently, enabling the CPU to switch context between threads efficiently, thus utilizing system resources better compared to a single-threaded approach . This concurrency model reduces idle time and can lead to faster execution for I/O-bound operations or tasks requiring frequent waiting, like network calls, while maintaining logical separation of tasks that can be processed simultaneously.

Custom exceptions in Java can be created by extending the Exception class. This allows developers to define exceptions that are specific to particular problem domains in their applications. For instance, a program may define a custom exception called 'NoMatchException' that is thrown when a certain string is not equal to 'India'. The custom exception is defined in a class extending Exception, and it can be used within a method that throws the exception when a condition is met . This allows for more precise and meaningful error handling tailored to specific application needs.

Creating custom exceptions, such as NoMatchException, is beneficial in scenarios where standard exceptions do not adequately describe an error situation specific to the application's domain. Custom exceptions can provide more meaningful error messages tailored to business logic, enhancing readability and maintainability. For example, in applications where input validation is crucial, a NoMatchException can specifically signal when an expected input like 'India' is not matched, enabling precise error handling and logging . This specificity aids in debugging and ensures that the application is handling unexpected values consistently with business requirements.

Catching specific exceptions like ArrayIndexOutOfBoundsException is crucial in maintaining program stability and providing insightful feedback to users or developers. It indicates an attempt to access an array with an illegal index, thus pinpointing a specific programming error. By catching this exception, developers can handle errors gracefully without crashing the program, display a meaningful error message, and take corrective action . This helps in debugging by providing precise information about the failure point in the code, which can improve reliability and user experience.

Java allows the assignment of priorities to threads to suggest the order of execution and resource allocation, though the actual impact depends on the underlying system's thread scheduler. Threads can have priorities ranging from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY, with Thread.NORM_PRIORITY being the default. Higher-priority threads are often scheduled to run before lower-priority ones, but this is not guaranteed . For instance, setting a thread's priority using setPriority allows emphasis on certain tasks over others but doesn't ensure they will always run in a particular order due to JVM and OS-level scheduling algorithms.

The try-catch block in Java is used to handle exceptions by allowing a block of code to be tested for errors while it is being executed. When an exception occurs within the try block, the control is passed to the catch block, where the exception is handled appropriately. For example, an ArithmeticException occurs when there is an illegal arithmetic operation, like division by zero . ArrayIndexOutOfBoundsException is caught when an invalid array index is accessed, such as trying to access an element out of the array's bounds . By using try-catch blocks, programmers can prevent the program from crashing and provide a meaningful response to the user.

Java's exception handling mechanism improves program robustness by allowing programs to handle errors gracefully, such as division by zero, without terminating abruptly. In Java, ArithmeticException is thrown when a division by zero is attempted. By using try-catch blocks, a program can catch this exception and execute alternative code, preventing a crash . This mechanism ensures that the application can continue to operate or shut down cleanly, providing users with feedback and allowing for recovery or debugging actions, which is key in building resilient software systems.

Multithreading in Java refers to the concurrent execution of two or more threads simultaneously. It is used to perform operations in parallel, improving the efficiency and performance of applications. An example of multithreading is a program that creates two threads: one that displays odd numbers from 1 to 50 and another that displays even numbers. Each thread runs independently, allowing the program to handle both operations concurrently . This is achieved by extending the Thread class or implementing the Runnable interface for each task, enabling simultaneous execution without interference.

You might also like