SlideShare a Scribd company logo
5.Handling Errors and Exceptions
Dr.Narayana Swamy Ramaiah
Assoc.Prof, Dept of Electrical and
Computer Engineering,
Arba Minch University
What is an Exception
• Exception is an event that occurs during execution of a program that disrupts
normal flow of instruction.
• Let's take a scenario:
instruction 1;
instruction 2;
instruction 3;
instruction 4;
instruction 5; //exception occurs
instruction 6;
instruction 7;
instruction 8;
instruction 9;
instruction 10;
• Suppose there is 10 instructions in your program and there occurs an exception at
instruction 5, rest of the code will not be executed i.e. instruction 6 to 10 will not
run.
• When an error occurs within a method, the method creates an exception object and hands it off to the
runtime system, this is called throwing an exception.
• After a method throws an exception, the runtime system attempts to find something to handle it. The set
of possible "somethings" to handle the exception is the ordered list of methods that had been called to
get to the method where the error occurred. The list of methods is known as the call stack (see the next
figure).
The call stack.
• The runtime system searches the call stack for a method that contains a block of code that can handle the
exception. This block of code is called an exception handler. The search begins with the method in which
the error occurred and proceeds through the call stack in the reverse order in which the methods were
called. When an appropriate handler is found, the runtime system passes the exception to the handler. An
exception handler is considered appropriate if the type of the exception object thrown matches the type
that can be handled by the handler.
• The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches
all the methods on the call stack without finding an appropriate exception handler, as shown in the next
figure, the runtime system (and, consequently, the program) terminates. Searching the call stack for the
exception handler.
Exception Types
• There are two types of exceptions
– Checked exceptions (checked at compile time)
• DivideByZero exception
• ClassNotFoundException
• IllegalAccessException
• NoSuchFieldException
• EOFException etc.
– Unchecked exceptions (checked at runtime)
• ArithmeticException
• ArrayIndexOutOfBoundsException
• NullPointerException
• NegativeArraySizeException etc.
• Error (irrecoverable-Thread Death)
• OutOfMemoryError,
• VirtualMachineError,
• AssertionError etc.
• All exception classes are subtypes of the
java.lang. Exception class.
• The exception class is a subclass of the
Throwable class. Other than the exception
class there is another subclass called Error
which is derived from the Throwable class.
• The Exception class has two main subclasses:
IOException class and RuntimeException Class.
Exceptions
• There are given some scenarios where unchecked exceptions can occur. They
are as follows:
• Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0; //ArithmeticException
• Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the
variable occurs an NullPointerException.
String s=null;
System.out.println(s.length()); //NullPointerException
• Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException.
Suppose I have a string variable that have characters, converting this
variable into digit will occur NumberFormatException.
String s="abc";
int i=Integer.parseInt(s); //NumberFormatException
• Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException

More Related Content

PPTX
Exception handling in java
PPTX
Exception handling
PPTX
Satish training ppt
PPTX
Java Exceptions and Exception Handling
PPT
9781439035665 ppt ch11
PPT
Chap12
PDF
Class notes(week 8) on exception handling
PPTX
Exception handling in java
Exception handling in java
Exception handling
Satish training ppt
Java Exceptions and Exception Handling
9781439035665 ppt ch11
Chap12
Class notes(week 8) on exception handling
Exception handling in java

What's hot (13)

PPTX
Exceptions in java
PPTX
Java Exception Handling
PDF
Exception handling basic
PPTX
Introduction to java exceptions
PPTX
Exception handling
PPTX
17 exceptions
PPTX
Exception handling in java
PPT
exception handling
PDF
Built in exceptions
PPT
PPT
Chapter13 exception handling
PPTX
exception handling in java
Exceptions in java
Java Exception Handling
Exception handling basic
Introduction to java exceptions
Exception handling
17 exceptions
Exception handling in java
exception handling
Built in exceptions
Chapter13 exception handling
exception handling in java
Ad

Similar to Exceptions (20)

PDF
Lesson 8 Handling Exceptions and Errors.pdf
PPTX
Java chapter 6
PPTX
Exception Handling s the process of responding to unwanted or unexpected even...
PPTX
Exception handling
PPTX
Java-Exception Handling Presentation. 2024
PPTX
Exception Handling In Java Presentation. 2024
PPTX
Exception handling in java.pptx
PPTX
Exceptionhandling
PPTX
EXCEPTION HANDLING in prograaming
PPT
A36519192_21789_4_2018_Exception Handling.ppt
PPT
Exception Handling Exception Handling Exception Handling
PPTX
Chap2 exception handling
PPTX
Java-Unit 3- Chap2 exception handling
PPTX
Java exception handling
PDF
16 exception handling - i
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
PPTX
Exception handling in java
PPTX
Exceptions overview
PPT
Java Exception.ppt
Lesson 8 Handling Exceptions and Errors.pdf
Java chapter 6
Exception Handling s the process of responding to unwanted or unexpected even...
Exception handling
Java-Exception Handling Presentation. 2024
Exception Handling In Java Presentation. 2024
Exception handling in java.pptx
Exceptionhandling
EXCEPTION HANDLING in prograaming
A36519192_21789_4_2018_Exception Handling.ppt
Exception Handling Exception Handling Exception Handling
Chap2 exception handling
Java-Unit 3- Chap2 exception handling
Java exception handling
16 exception handling - i
UNIT III 2021R.pptx
UNIT III 2021R.pptx
Exception handling in java
Exceptions overview
Java Exception.ppt
Ad

More from Narayana Swamy (7)

PDF
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
PPTX
Files io
PDF
Z blue introduction to gui (39023299)
PPTX
Z blue interfaces and packages (37129912)
PPTX
Z blue exception
PPTX
Oop inheritance chapter 3
PPTX
Exceptions
AICTE GUIDLINES AICTE GUIDLINESAICTE GUIDLINES
Files io
Z blue introduction to gui (39023299)
Z blue interfaces and packages (37129912)
Z blue exception
Oop inheritance chapter 3
Exceptions

Recently uploaded (20)

PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
introduction to high performance computing
PPTX
Artificial Intelligence
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Management Information system : MIS-e-Business Systems.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
Feature types and data preprocessing steps
PDF
Design Guidelines and solutions for Plastics parts
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PDF
Visual Aids for Exploratory Data Analysis.pdf
PPTX
Current and future trends in Computer Vision.pptx
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
PPTX
communication and presentation skills 01
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Fundamentals of Mechanical Engineering.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
distributed database system" (DDBS) is often used to refer to both the distri...
introduction to high performance computing
Artificial Intelligence
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Management Information system : MIS-e-Business Systems.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Feature types and data preprocessing steps
Design Guidelines and solutions for Plastics parts
R24 SURVEYING LAB MANUAL for civil enggi
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Visual Aids for Exploratory Data Analysis.pdf
Current and future trends in Computer Vision.pptx
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
22EC502-MICROCONTROLLER AND INTERFACING-8051 MICROCONTROLLER.pdf
communication and presentation skills 01
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf

Exceptions

  • 1. 5.Handling Errors and Exceptions Dr.Narayana Swamy Ramaiah Assoc.Prof, Dept of Electrical and Computer Engineering, Arba Minch University
  • 2. What is an Exception • Exception is an event that occurs during execution of a program that disrupts normal flow of instruction. • Let's take a scenario: instruction 1; instruction 2; instruction 3; instruction 4; instruction 5; //exception occurs instruction 6; instruction 7; instruction 8; instruction 9; instruction 10; • Suppose there is 10 instructions in your program and there occurs an exception at instruction 5, rest of the code will not be executed i.e. instruction 6 to 10 will not run.
  • 3. • When an error occurs within a method, the method creates an exception object and hands it off to the runtime system, this is called throwing an exception. • After a method throws an exception, the runtime system attempts to find something to handle it. The set of possible "somethings" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack (see the next figure). The call stack. • The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler. The search begins with the method in which the error occurred and proceeds through the call stack in the reverse order in which the methods were called. When an appropriate handler is found, the runtime system passes the exception to the handler. An exception handler is considered appropriate if the type of the exception object thrown matches the type that can be handled by the handler. • The exception handler chosen is said to catch the exception. If the runtime system exhaustively searches all the methods on the call stack without finding an appropriate exception handler, as shown in the next figure, the runtime system (and, consequently, the program) terminates. Searching the call stack for the exception handler.
  • 4. Exception Types • There are two types of exceptions – Checked exceptions (checked at compile time) • DivideByZero exception • ClassNotFoundException • IllegalAccessException • NoSuchFieldException • EOFException etc. – Unchecked exceptions (checked at runtime) • ArithmeticException • ArrayIndexOutOfBoundsException • NullPointerException • NegativeArraySizeException etc. • Error (irrecoverable-Thread Death) • OutOfMemoryError, • VirtualMachineError, • AssertionError etc.
  • 5. • All exception classes are subtypes of the java.lang. Exception class. • The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. • The Exception class has two main subclasses: IOException class and RuntimeException Class.
  • 7. • There are given some scenarios where unchecked exceptions can occur. They are as follows: • Scenario where ArithmeticException occurs If we divide any number by zero, there occurs an ArithmeticException. int a=50/0; //ArithmeticException • Scenario where NullPointerException occurs If we have null value in any variable, performing any operation by the variable occurs an NullPointerException. String s=null; System.out.println(s.length()); //NullPointerException
  • 8. • Scenario where NumberFormatException occurs The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException. String s="abc"; int i=Integer.parseInt(s); //NumberFormatException • Scenario where ArrayIndexOutOfBoundsException occurs If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below: int a[]=new int[5]; a[10]=50; //ArrayIndexOutOfBoundsException