SlideShare a Scribd company logo
Exception Handling and Multithreading
UNIT 3
UNIT 3-TOPICS
 Exception Handling Basics
 Multiple Catch Clauses
 Nested try statements
 Java’s Built-in Exception
 User defined Exception
UNIT 3-TOPICS
 Multithreaded Programming: Java Thread Model
 Creating a thread and multiple threads
 Thread Priorities
 Synchronization-Interthread communication
 Suspending, Resuming and Stopping Threads-Multithreading
 Wrappers-Autoboxing
Exception Handling Basics
Exception is an abnormal event that arises during the
execution of the program and disrupts the normal flow of the
program.
Exception Handling is a mechanism to handle the runtime
errors so that the normal flow of the application can be
maintained.
Exception Handling Basics
There are various types of interruptions while executing any
program like errors, exceptions, and bugs.
These interruptions can be due to programming mistakes or
due to system issues.
Depending on the conditions they can be classified as
1.Exception
2.Errors
Exception
Exceptions are unwanted conditions that disrupt the flow of
the program.
Exceptions usually occur due to the code and can be
recovered.
Exceptions can be of both checked (exceptions that are
checked by the compiler) and unchecked (exceptions that
cannot be checked by the compiler) type.
Exception
They can occur at both compile time and run time.
In Java, exceptions belong to java.lang.Exception class.
Checked Exception
Checked exceptions are those exceptions that are checked at
compile time by the compiler.
The program will not compile if they are not handled.
These exceptions are child classes of the Exception class.
IOException, ClassNotFoundException, SQL Exception are
a few of the checked exceptions in Java.
UnChecked Exception
UnChecked exceptions are those exceptions that are checked
at run time by JVM, as the compiler cannot check
unchecked exception.
The programs with unchecked exceptions get compiled
successfully but they give runtime errors if not handled.
These are child classes of Runtime Exception Class.
UnChecked Exception
Arithmetic Exceptions, NullPointerException,
NumberFormatException, IndexOutOfBoundException are
a few of the unchecked exceptions in Java.
Error
An error is also an unwanted condition but it is caused due
to lack of resources and indicates a serious problem.
Errors are irrecoverable, they cannot be handled by the
programmers. Eg: OutOfMemoryError.
Errors are of unchecked type only.
They can occur only at run time.
In java, errors belong to java.lang.Error class.
UNIT-3.pptx Exception Handling and Multithreading
Execution Flow
The JVM firstly checks whether the exception is handled or
not. If exception is not handled, JVM provides a default
exception handler that performs the following tasks:
 Print out exception description.
 Prints the stack trace
 Causes the program to terminate
But if the application programmer handles the exception, the
normal flow of the application is maintained, i.e., rest of
the code is executed.
Checked Exception Vs UnChecked Exception
Checked Exception UnChecked Exception
Checked Exception occur at compile time UnChecked Exception Occur at run time
The compiler checks for checked exception The compiler does not check for
unchecked exception
If checked exceptions are not handled, then
get compile time error
If unchecked exceptions are not handled,
then get run time error
Checked Exceptions are direct subclasses of
Exception but do not inherit Runtime
Exception class
UnChecked Exceptions are subclasses of
the Runtime Exception class
Example: IOException,
ClassNotFoundException, SQLException
Example: ArithmeticException,
NullPointerException,
ArrayIndexOutofBoundxception
Java Exception Keywords
Exception handling is managed via five keywords: try, catch, throw,
throws, and finally
 try block: contains the program statements that may raise an
exception. The try block must be followed by either catch or finally.
 catch block: is used to handle the raised exception. It must be
preceded by try block and can be followed by finally block later.
Java Exception Keywords
 throw keyword: is used to explicitly throw an exception from a
method or any block of code.
 throws keyword: is used to declare that a method may throw a
specific exception.
 finally block: contains statement that must be executed after the
try block. finally block code is used to release resources such as
closing files or database connections.
try
{
//code that may throw an exception
}
catch(ExceptionClassName ref)
{
//code that may throw an exception
}
try-catch block-syntax
try
{
//code that may throw an exception
}
finally
{
//code
}
try-finally block-syntax
finally block
finally block is a block used to execute important code such
closing the database connection.
 Java code within the finally block is always executed
whether an exception is handled or not. Therefore, it
contains all the necessary statements that need to be printed
regardless of the exception occurs or not.
The finally block follows the try-catch block.
UNIT-3.pptx Exception Handling and Multithreading
Multiple catch clauses
A try block can be followed by one or more catch blocks.
Each catch block must contain a different exception handler. Hence multi-
catch block is used to perform different tasks at the occurrence of different
exceptions.
At a time only one exception occurs and at a time only one catch block is
executed.
All catch blocks must be ordered from most specific to more general
i.e., catch for Arithmetic Exception must come before catch for Exception.
UNIT-3.pptx Exception Handling and Multithreading
Multiple Catch Block
class Muticatchdemo
{
public static void main(String args[])
{
try
{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println(“Arithmetic exception occurs:”+e);
}
Multiple Catch Block
catch(ArrayIndexOutofBoundsException e)
{
System.out.println(“Array index out of bounds exception occurs:”+e);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println(“rest of code executes”);
}
}
Multithreading Programming
Multitasking is a process of executing multiple tasks
simultaneously. The concept of multitasking is to utilize the
CPU at its Maximum. Multitasking can be achieved in two
ways
 Process-based Multitasking (Multiprocessing)
 Thread-based Multitasking (Multithreading)
Process
 Process simply means a program in execution.
 Processes have their own separate address space (their
own code & data) & use more resources and hence they
are termed as heavyweight process.
UNIT-3.pptx Exception Handling and Multithreading
Thread
 Thread is a sequential flow of tasks within a process.
Threads are used to increase the performance of
application.
 Each Thread has its own Program counter, stack and set of
registers. But the threads of a single process might share
the same code and data/file. Threads are also called as
light-weight processes as they share common resources.
UNIT-3.pptx Exception Handling and Multithreading

More Related Content

PPTX
Multithreading in Java
PPTX
JAVA PROGRAMMING
PPT
SQL subquery
PPTX
Sparse matrix and its representation data structure
PPT
Thread model in java
PPTX
interface in c#
PPT
Packages,interfaces and exceptions
PPT
Types of exceptions
Multithreading in Java
JAVA PROGRAMMING
SQL subquery
Sparse matrix and its representation data structure
Thread model in java
interface in c#
Packages,interfaces and exceptions
Types of exceptions

What's hot (20)

PPTX
software engineering
PPT
Java interfaces
PPTX
DIAGRAMA DE COMPONENTES
PPTX
Interface in java
PDF
Jdbc connectivity in java
PPTX
Sql(structured query language)
PPTX
Interfaces in java
PPTX
Control statements in java
PPT
Java layoutmanager
PPT
Structure and Enum in c#
PPTX
set operators.pptx
PPTX
Object-Oriented Programming with Java UNIT 1
PPTX
Inter Thread Communicationn.pptx
PPTX
collection framework in java
PPTX
Classes and objects
PPTX
Vectors in Java
PPTX
Oracle: Control Structures
PPTX
Estructura de datos Pilas, Colas y Listas.
PPT
Basic using of Swing in Java
PPTX
SQL Functions
software engineering
Java interfaces
DIAGRAMA DE COMPONENTES
Interface in java
Jdbc connectivity in java
Sql(structured query language)
Interfaces in java
Control statements in java
Java layoutmanager
Structure and Enum in c#
set operators.pptx
Object-Oriented Programming with Java UNIT 1
Inter Thread Communicationn.pptx
collection framework in java
Classes and objects
Vectors in Java
Oracle: Control Structures
Estructura de datos Pilas, Colas y Listas.
Basic using of Swing in Java
SQL Functions
Ad

Similar to UNIT-3.pptx Exception Handling and Multithreading (20)

PPTX
java exception.pptx
PPTX
Java exception handling
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
PDF
Java Day-5
PPT
Exception Handling in JAVA
PDF
JAVA PROGRAMMING- Exception handling - Multithreading
PPT
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
PPTX
Chap2 exception handling
PPTX
Java-Unit 3- Chap2 exception handling
PPT
Exception handling
PPTX
JAVA UNIT 2
PPTX
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
PPTX
Exception Handling.pptx
PPTX
using Java Exception Handling in Java.pptx
PPTX
Exception handling in java
PPTX
Exception handling in java
PPTX
Exception handling in java
PDF
Ch-1_5.pdf this is java tutorials for all
java exception.pptx
Java exception handling
UNIT III 2021R.pptx
UNIT III 2021R.pptx
Java Day-5
Exception Handling in JAVA
JAVA PROGRAMMING- Exception handling - Multithreading
oop-unit-iii-ppt.pptexceptionhandlingobjectorientedprogramming
Chap2 exception handling
Java-Unit 3- Chap2 exception handling
Exception handling
JAVA UNIT 2
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Exception Handling.pptx
using Java Exception Handling in Java.pptx
Exception handling in java
Exception handling in java
Exception handling in java
Ch-1_5.pdf this is java tutorials for all
Ad

More from SakkaravarthiS1 (6)

PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
PPT
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
PDF
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
PDF
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
PDF
UNIT4-IO,Generics,String Handling.pdf Notes
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
UNIT-1 PPT.pptCS 3492 DBMS UNIT 1 to 5 overview Unit 1 slides including purpo...
DATABASE MANAGEMENT SYSTEMS university course materials useful for students ...
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
UNIT4-IO,Generics,String Handling.pdf Notes

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPT
Project quality management in manufacturing
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
composite construction of structures.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Well-logging-methods_new................
Project quality management in manufacturing
CH1 Production IntroductoryConcepts.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
additive manufacturing of ss316l using mig welding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Internet of Things (IOT) - A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
Digital Logic Computer Design lecture notes
CYBER-CRIMES AND SECURITY A guide to understanding
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
573137875-Attendance-Management-System-original
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks

UNIT-3.pptx Exception Handling and Multithreading

  • 1. Exception Handling and Multithreading UNIT 3
  • 2. UNIT 3-TOPICS  Exception Handling Basics  Multiple Catch Clauses  Nested try statements  Java’s Built-in Exception  User defined Exception
  • 3. UNIT 3-TOPICS  Multithreaded Programming: Java Thread Model  Creating a thread and multiple threads  Thread Priorities  Synchronization-Interthread communication  Suspending, Resuming and Stopping Threads-Multithreading  Wrappers-Autoboxing
  • 4. Exception Handling Basics Exception is an abnormal event that arises during the execution of the program and disrupts the normal flow of the program. Exception Handling is a mechanism to handle the runtime errors so that the normal flow of the application can be maintained.
  • 5. Exception Handling Basics There are various types of interruptions while executing any program like errors, exceptions, and bugs. These interruptions can be due to programming mistakes or due to system issues. Depending on the conditions they can be classified as 1.Exception 2.Errors
  • 6. Exception Exceptions are unwanted conditions that disrupt the flow of the program. Exceptions usually occur due to the code and can be recovered. Exceptions can be of both checked (exceptions that are checked by the compiler) and unchecked (exceptions that cannot be checked by the compiler) type.
  • 7. Exception They can occur at both compile time and run time. In Java, exceptions belong to java.lang.Exception class.
  • 8. Checked Exception Checked exceptions are those exceptions that are checked at compile time by the compiler. The program will not compile if they are not handled. These exceptions are child classes of the Exception class. IOException, ClassNotFoundException, SQL Exception are a few of the checked exceptions in Java.
  • 9. UnChecked Exception UnChecked exceptions are those exceptions that are checked at run time by JVM, as the compiler cannot check unchecked exception. The programs with unchecked exceptions get compiled successfully but they give runtime errors if not handled. These are child classes of Runtime Exception Class.
  • 10. UnChecked Exception Arithmetic Exceptions, NullPointerException, NumberFormatException, IndexOutOfBoundException are a few of the unchecked exceptions in Java.
  • 11. Error An error is also an unwanted condition but it is caused due to lack of resources and indicates a serious problem. Errors are irrecoverable, they cannot be handled by the programmers. Eg: OutOfMemoryError. Errors are of unchecked type only. They can occur only at run time. In java, errors belong to java.lang.Error class.
  • 14. The JVM firstly checks whether the exception is handled or not. If exception is not handled, JVM provides a default exception handler that performs the following tasks:  Print out exception description.  Prints the stack trace  Causes the program to terminate But if the application programmer handles the exception, the normal flow of the application is maintained, i.e., rest of the code is executed.
  • 15. Checked Exception Vs UnChecked Exception Checked Exception UnChecked Exception Checked Exception occur at compile time UnChecked Exception Occur at run time The compiler checks for checked exception The compiler does not check for unchecked exception If checked exceptions are not handled, then get compile time error If unchecked exceptions are not handled, then get run time error Checked Exceptions are direct subclasses of Exception but do not inherit Runtime Exception class UnChecked Exceptions are subclasses of the Runtime Exception class Example: IOException, ClassNotFoundException, SQLException Example: ArithmeticException, NullPointerException, ArrayIndexOutofBoundxception
  • 16. Java Exception Keywords Exception handling is managed via five keywords: try, catch, throw, throws, and finally  try block: contains the program statements that may raise an exception. The try block must be followed by either catch or finally.  catch block: is used to handle the raised exception. It must be preceded by try block and can be followed by finally block later.
  • 17. Java Exception Keywords  throw keyword: is used to explicitly throw an exception from a method or any block of code.  throws keyword: is used to declare that a method may throw a specific exception.  finally block: contains statement that must be executed after the try block. finally block code is used to release resources such as closing files or database connections.
  • 18. try { //code that may throw an exception } catch(ExceptionClassName ref) { //code that may throw an exception } try-catch block-syntax
  • 19. try { //code that may throw an exception } finally { //code } try-finally block-syntax
  • 20. finally block finally block is a block used to execute important code such closing the database connection.  Java code within the finally block is always executed whether an exception is handled or not. Therefore, it contains all the necessary statements that need to be printed regardless of the exception occurs or not. The finally block follows the try-catch block.
  • 22. Multiple catch clauses A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. Hence multi- catch block is used to perform different tasks at the occurrence of different exceptions. At a time only one exception occurs and at a time only one catch block is executed. All catch blocks must be ordered from most specific to more general i.e., catch for Arithmetic Exception must come before catch for Exception.
  • 24. Multiple Catch Block class Muticatchdemo { public static void main(String args[]) { try { int a[]=new int[5]; a[5]=30/0; } catch(ArithmeticException e) { System.out.println(“Arithmetic exception occurs:”+e); }
  • 25. Multiple Catch Block catch(ArrayIndexOutofBoundsException e) { System.out.println(“Array index out of bounds exception occurs:”+e); } catch(Exception e) { System.out.println(e); } System.out.println(“rest of code executes”); } }
  • 26. Multithreading Programming Multitasking is a process of executing multiple tasks simultaneously. The concept of multitasking is to utilize the CPU at its Maximum. Multitasking can be achieved in two ways  Process-based Multitasking (Multiprocessing)  Thread-based Multitasking (Multithreading)
  • 27. Process  Process simply means a program in execution.  Processes have their own separate address space (their own code & data) & use more resources and hence they are termed as heavyweight process.
  • 29. Thread  Thread is a sequential flow of tasks within a process. Threads are used to increase the performance of application.  Each Thread has its own Program counter, stack and set of registers. But the threads of a single process might share the same code and data/file. Threads are also called as light-weight processes as they share common resources.