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
Intermediate code generator
PPTX
Recursion
PPT
linked list
PPT
Code generator
PPTX
DBMS (UNIT 2)
PPTX
Dynamic storage allocation techniques
PPTX
Principal source of optimization in compiler design
PPT
Context free grammars
Intermediate code generator
Recursion
linked list
Code generator
DBMS (UNIT 2)
Dynamic storage allocation techniques
Principal source of optimization in compiler design
Context free grammars

What's hot (20)

PPTX
Algorithm and pseudo codes
PPTX
PDF
Code generation in Compiler Design
PDF
Lecture Note-1: Algorithm and Its Properties
PDF
Syntax analysis
PPT
Branching in C
PDF
Problem Solving and Python Programming
PPTX
Open addressiing &rehashing,extendiblevhashing
PPT
Basic elements of java
PPTX
Basic Input and Output
PPTX
Hashing_UNIT2.pptx
PPTX
Pseudocode
PPT
LR-Parsing.ppt
ODP
Partitioning
PDF
Operator Precedence Grammar
PPTX
Circular linked list
PPTX
Queues in C++
PPTX
PPT
Minimum spanning tree
Algorithm and pseudo codes
Code generation in Compiler Design
Lecture Note-1: Algorithm and Its Properties
Syntax analysis
Branching in C
Problem Solving and Python Programming
Open addressiing &rehashing,extendiblevhashing
Basic elements of java
Basic Input and Output
Hashing_UNIT2.pptx
Pseudocode
LR-Parsing.ppt
Partitioning
Operator Precedence Grammar
Circular linked list
Queues in C++
Minimum spanning tree
Ad

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

PPTX
Exception Handling.pptx
PPTX
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
PPTX
Exception handling in java
PPTX
Exception handling in java
PPTX
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
DOCX
Class notes(week 8) on exception handling
PPTX
UNIT 2.pptx
PDF
Exception handling basic
PDF
Class notes(week 8) on exception handling
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Java-Exception Handling Presentation. 2024
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PDF
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
PPTX
unit 4 msbte syallbus for sem 4 2024-2025
DOCX
Exception handling in java
PPTX
Exception handling in java
PPTX
Exception handling in java
PPTX
Java -Exception handlingunit-iv
PPTX
Exception handling
PDF
Ch-1_5.pdf this is java tutorials for all
Exception Handling.pptx
OBJECT ORIENTED PROGRAMMING_Unit3_NOTES first half.pptx
Exception handling in java
Exception handling in java
Exception Handling Multithreading: Fundamental of Exception; Exception types;...
Class notes(week 8) on exception handling
UNIT 2.pptx
Exception handling basic
Class notes(week 8) on exception handling
Exception Handling In Java Presentation. 2024
Java-Exception Handling Presentation. 2024
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
JAVA UNIT-2 ONE SHOT NOTES_64156529_2025_07_12_10__250712_103642.pdf
unit 4 msbte syallbus for sem 4 2024-2025
Exception handling in java
Exception handling in java
Exception handling in java
Java -Exception handlingunit-iv
Exception handling
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
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
web development for engineering and engineering
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Well-logging-methods_new................
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Sustainable Sites - Green Building Construction
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
web development for engineering and engineering
Safety Seminar civil to be ensured for safe working.
III.4.1.2_The_Space_Environment.p pdffdf
CYBER-CRIMES AND SECURITY A guide to understanding
Well-logging-methods_new................
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
CH1 Production IntroductoryConcepts.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Sustainable Sites - Green Building Construction
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Fundamentals of safety and accident prevention -final (1).pptx
Geodesy 1.pptx...............................................
Internet of Things (IOT) - A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

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.