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
• 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
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
4
04/12/2018
5
04/12/2018
• 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
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
8
04/12/2018
Exception Hierarchy
Package
Root Class
9
04/12/2018
10
04/12/2018
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
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
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
15