Anda di halaman 1dari 15

04/12/2018

Unit -9 Exception Handling


Chapter 11 – Java How to Program

Topics to be covered
• Exception Handling Fundamentals
• Exception Types
• Try and Catch
• Nested Try Statements
• Throw, Throws, Finally
• Java’s Built-in Exceptions
• Creating Your Own Exception Subclasses

1
04/12/2018

Exception Handling - Fundamentals

Exception
• What is an Exception?
• A run time error
• Exception Examples:
• Divided by Zero
• Invalid Input
• File Not Found
• Array out of bound
• Input Mismatch
• Access Denied – No Read /Write Permissions
• Consequences
• The program terminates abruptly – CRASH!!!
• Not a good user experience
• Is there a graceful way to exit?
• Yes – Exception Handling

2
04/12/2018

Some Program Crash Examples

Exception Handling
• What is it?
• The process of handling exceptions( run-time errors) to avoid abrupt termination
• What are its advantages?
• Exception Handling allows a program to execute as if no problem has been encountered
• Fault tolerant programs
• If not able to continue executing– at least terminate gracefully
• Let the user know what has occurred and what is going to happen next
• Sometimes you are diverted to some point earlier to code that caused an exception
• You entered a character instead of an integer,
• An Exception occurs , if you handle it, one is given a chance to re-enter the correct input
• Java Exception Handling
• Is based on exception handling in C++

3
04/12/2018

A program which may throw Exceptions

What Exceptions can occur ?? ( in Program on Previous Slide)


• A successful Execution

4
04/12/2018

What Exceptions can occur ?? ( in Program on Slide#7)


• What if I provide 0 as the denominator ??

What Exceptions can occur ?? ( in Program on Slide#7)


• What if I provide a char instead of an integer ??

5
04/12/2018

What Exceptions can occur ?? ( in Program on Slide#7)


• What if I provide a float value instead of an Integer??

Exceptions : Program on Slide#7

• ArithmeticException
• Thrown when an exception condition occurs in an arithmetic operation
• For Example / by Zero
• InputMismatchException
• Occurs when the provided input does not match the expected input
• For Example : Java Expects an integer and you provided a character or float

6
04/12/2018

What happens when an Exception occurs ?

• A Java exception is an object that represents an error condition


• When an exception ( error) occurs :
• An objects is created and thrown in the method that caused that error
• That method may choose to handle the exception itself, or pass it on
• Either way, at some pint exception is caught and processed
• We can choose to handle exception
• Using a try –catch block
• Just use Throws keyword with known exceptions that declares that the
method being called is a risky one
• If neither of two is done, the compiler will give an error

Exception Types

7
04/12/2018

Types of Exception
• Why Exceptions occur?
• Due to user error
• Due to errors in programming on part of the programmer
• Due to limitations of physical resources
• Types of Exceptions
• Checked Exceptions
• Un-Checked Exceptions
• Errors

Types of Exceptions https://www.tutorialspoint.com/java/java_exceptions.htm


• Checked Exception
• That is checked (notified) by the compiler at compilation-time
• These are also called as compile time exceptions.
• These exceptions cannot simply be ignored, the programmer should take care of (handle)
these exceptions.
• Un-Checked Exceptions
• An unchecked exception is an exception that occurs at the time of execution.
• These are also called as Runtime Exceptions.
• Runtime exceptions are ignored at the time of compilation.
• These include programming bugs, such as logic errors or improper use of an API.
• Errors −
• Problems that arise beyond the control of the user or the programmer.
• For example, if a stack overflow occurs, an error will arise.
• They are also ignored at the time of compilation.

8
04/12/2018

Exception Hierarchy

Package

Root Class

Exception Super Class

All Exceptions are sub classes of


Throwable

Exception Types – Built-in Exceptions

9
04/12/2018

Exception Types – Built-in Exceptions

try and catch ; throw, throws and


finally

10
04/12/2018

A program which may throw Exceptions

Lets Handle These Exceptions !!

11
04/12/2018

Sample Run

throw keyword
• throw
• throw keyword is used to explicitly throw an exception
• We can throw a checked as well as an un-checked exception using throw keyword
• We use throw to throw custom exceptions
• For Example:
• If a user enters his password which is same as his name
• If a user cant access an application if his age is <15
• If a player needs one to quit in a Ludo game but he gets score other than one
• In all above cases , we can create custom exceptions to enforce these rules
• Custom Exceptions are user defined exceptions .
• If we do not enforce such exceptions , Java will not throw such exception
• All custom exceptions need to be instantiated as an object of type throwable or a
subclass of throwable

12
04/12/2018

throw Keyword Example


ABC School is selecting students
for a 100 metre race . Selection
criteria is very simple :

Age<12 && weight<40

Students which do not satisfy


either of the condition will not be
registered for the race.

throws Keyword
• throws
• throws keyword is used to declare an exception
• It is used to declare checked ( compile time) exceptions
• throws is followed by one or more exceptions that may occur in the method
• throws delegates the responsibility of exception handling to the caller of the program

• When an exception is declared using throws it must be handled in the calling function
• Otherwise JVM will alert a compilation error

13
04/12/2018

Difference Between throw and throws

finally block

• A finally block
• The statements present in this block will always execute regardless of whether
exception occurs in try block or not
• such as closing a connection, file etc.

14
04/12/2018

finally Block – Example

finally – Execution Flow

15

Anda mungkin juga menyukai