Topic 4 Exception Handling Part I
-
Upload
pabburahati -
Category
Documents
-
view
221 -
download
0
Transcript of Topic 4 Exception Handling Part I
-
7/30/2019 Topic 4 Exception Handling Part I
1/19
Programming Language (JAVA)
Unit 6.10 Exception Handling
Presentation1
-
7/30/2019 Topic 4 Exception Handling Part I
2/19
Revision
1. What are the two types of packages?
2. List some of the Java packages.
3. What is a user-defined package?
-
7/30/2019 Topic 4 Exception Handling Part I
3/19
Objectives
At the end of this presentation, you will be able to : Define the concept of Exception Handling
Mechanism
Define the use of try, catch and throw keywords.
-
7/30/2019 Topic 4 Exception Handling Part I
4/19
Error Handling
Errors are common during programming
They result in
Wrong Output
Abrupt termination of the program
Crashing of the system
These problems when left undetected, willcause big problems.
Java provides error handling mechanism tosolve these problems.
-
7/30/2019 Topic 4 Exception Handling Part I
5/19
Exceptions in Java
Exceptions are handled in Java using the fivekeywords
try
catch
throw
throws
finally
-
7/30/2019 Topic 4 Exception Handling Part I
6/19
Exception Handling Mechanism
tryblock
catchblock
finally
block
throw
Exception
-
7/30/2019 Topic 4 Exception Handling Part I
7/19
Types of Errors
Generally, in any language there are twobroad classification of errors:
Compile Time errors Errors that occur due to wrong syntax in the
program.
Detected by compilers and .class file not created.
Runtime errors
Errors that occur while executing a program.
The .class file are created but may not runproperly.
-
7/30/2019 Topic 4 Exception Handling Part I
8/19
Hands-On!
Program compileerrordemo.javademonstrates a compile time error.
-
7/30/2019 Topic 4 Exception Handling Part I
9/19
Activity 6.10.1
Correct the errors in the following program
and execute it:
Step 1: Open the file error1.java file.Step 2: Compile the program and observe the
errors.
Step 3: Correct the errors and save theprogram.
Step 4: Compile and execute the program.
-
7/30/2019 Topic 4 Exception Handling Part I
10/19
Hands-On!
Program runerrordemo.java demonstrates aruntime error.
-
7/30/2019 Topic 4 Exception Handling Part I
11/19
Activity 6.10.2
Identify why an error occurs while running
the following program:
Step 1: Open the file error2. java file.
Step 2: Compile the program and execute it.
Step 3: Observe the runtime error that occurs.
Step 4: Mention the cause for the error.
-
7/30/2019 Topic 4 Exception Handling Part I
12/19
Need for Exception Handling
It is a very important mechanism in all theprogramming languages.
The needs are :
To avoid abnormal program termination.
To avoid system crashes.
It helps to detect and report the exceptionalcircumstances, in order to take a necessaryaction.
-
7/30/2019 Topic 4 Exception Handling Part I
13/19
Types of Exceptions
Exception Type DescriptionArithmetic
Exception
Occurs when an abnormal
arithmetic condition occurs.ArrayIndexOutOf
BoundsException
Occurs when a program tries to
access an array element whose
index value is out of range
NumberFormat
Exception
Occurs when you try to convert a
string, which is not in the form of
a number into numeric.
-
7/30/2019 Topic 4 Exception Handling Part I
14/19
Exception Handling
Java provides superior support for exception
handling.
Java exception can be manually generated
by the code
by the Java runtime system during the program
execution.
-
7/30/2019 Topic 4 Exception Handling Part I
15/19
Throwable Sub classes
-
7/30/2019 Topic 4 Exception Handling Part I
16/19
try,catch, throw blocks
An exception is an object, which is generated
during the execution of the program.
When an exception arises an exceptionobject of that exception class is created and
thrown.
The exception which is thrown can be caughtinside the program to stop the abnormal
program termination.
-
7/30/2019 Topic 4 Exception Handling Part I
17/19
Summary
In this presentation, you learnt the following:
Exception handling is a very important
mechanism adopted by the programmingfield.
Generally in any language the errors are
broadly classified into two categories. Runtime errors are the errors which occur
during the execution period.
-
7/30/2019 Topic 4 Exception Handling Part I
18/19
Summary
In this presentation, you learnt the following:
Java exception can be manually generated
by the code or the Java runtime system cangenerate exception during the programexecution.
The exception types are subclass of theclass Throwable.
The class Throwable has two subclassesnamely Error and Exception.
-
7/30/2019 Topic 4 Exception Handling Part I
19/19
Assignment
1. Define exception.