0% found this document useful (0 votes)
15 views

Exception Handling 0 Multi Threading

Exception handling in Java is used to deal with runtime errors by using try, catch, and finally blocks. Exceptions represent errors using Exception class objects. Multithreading allows executing multiple threads concurrently using Thread class or Runnable interface.

Uploaded by

animehv5500
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Exception Handling 0 Multi Threading

Exception handling in Java is used to deal with runtime errors by using try, catch, and finally blocks. Exceptions represent errors using Exception class objects. Multithreading allows executing multiple threads concurrently using Thread class or Runnable interface.

Uploaded by

animehv5500
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception Handling

Exception handling in Java is a mechanism used to deal with runtime errors, which are unexpected
conditions that occur during the execution of a program. By handling exceptions gracefully, you can
prevent your program from crashing and provide meaningful feedback to the user.

In Java, exceptions are represented by objects derived from the java.lang.Exception class or one of its
subclasses. The basic structure of exception handling in Java involves the following keywords:

try: This block encloses the code where an exception might occur.

catch: This block catches the exception thrown in the try block. You can have multiple catch blocks to
handle different types of exceptions.

finally: This block, if present, is executed regardless of whether an exception occurs or not. It's often
used for cleanup tasks like closing files or releasing resources.

Here's a basic example of exception handling in Java:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

try {

System.out.print("Enter a number: ");

int num = scanner.nextInt();

int result = 10 / num;

System.out.println("Result: " + result);

} catch (ArithmeticException e) {

System.out.println("Error: Division by zero is not allowed.");

} catch (Exception e) {

System.out.println("An error occurred: " + e.getMessage());

} finally {

scanner.close();

System.out.println("Program execution completed.");

}
}

In this example:

We try to divide 10 by the number entered by the user.

If the user enters 0, it throws an ArithmeticException, which is caught by the first catch block.

If any other exception occurs, it is caught by the second catch block.

The finally block is executed regardless of whether an exception occurs or not.

Remember that it's essential to handle exceptions appropriately in your code to make it more robust and
user-friendly. This includes providing meaningful error messages and handling exceptions at appropriate
levels in your application.

Multi-Threading

Multithreading in Java allows you to write programs that execute multiple threads concurrently, enabling
better utilization of CPU resources and improving program responsiveness. Java provides built-in support
for multithreading through its java.lang.Thread class and the java.lang.Runnable interface. Here's a basic
overview of how multithreading works in Java:

Using the Thread class: You can create a new thread by extending the Thread class and overriding its
run() method. Then, you can instantiate your custom thread class and start the thread using the start()
method.

class MyThread extends Thread {

public void run() {

// Code to be executed by the thread

System.out.println("Thread running...");

public class Main {

public static void main(String[] args) {

MyThread thread = new MyThread();


thread.start(); // Start the thread

Using the Runnable interface: Alternatively, you can implement the Runnable interface and provide the
implementation of the run() method. Then, you can create a Thread object by passing an instance of
your Runnable implementation to its constructor.

class MyRunnable implements Runnable {

public void run() {

// Code to be executed by the thread

System.out.println("Runnable running...");

public class Main {

public static void main(String[] args) {

MyRunnable myRunnable = new MyRunnable();

Thread thread = new Thread(myRunnable);

thread.start(); // Start the thread

You might also like