What is an Exception?
● Exceptional event● Error that occurs during runtime● Cause normal program flow to be disrupted● Examples- – Divide by zero errors
- – Accessing the elements of an array beyond its range
- – Invalid input
- – Hard disk crash
- – Opening a non-existent file
- – Heap memory exhausted
What Happens When an Exception Occurs?
● When an exception occurs within a method, the method creates an exception object and hands it off to the runtime system
– Creating an exception object and handing it to the runtime system is called “throwing an exception”
– Exception object contains information about the error, including its type and the state of the program when the error occurred
Benefits of Java Exception Handling Framework
● Separating Error-Handling code from “regular” business logic code
● Propagating errors up the call stack
● Grouping and differentiating error types
The finally Keyword
● Block of code is always executed despite of different scenarios:
– Forced exit occurs using a return, a continue or a break statement
– Normal completion
– Caught exception thrown
● Exception was thrown and caught in the method
– Uncaught exception thrown
● Exception thrown was not specified in any catch block in the method
Throwing Exceptions: The throw Keyword
● Java allows you to throw exceptions (generate exceptions)
throw <exception object>;
● An exception you throw is an object
– You have to create an exception object in the same way you create any other object
● Example:
throw new ArithmeticException(“testing...”);
Exception Class Hierarchy
The Error and Exception Classes
Throwable class
- Root class of exception classes
- Immediate subclasses
- Error
- Exception
Exception class
– Conditions that user programs can reasonably deal with
– Usually the result of some flaws in the user program code
– Examples
- Division by zero error
- Array out-of-bounds error
Error class
– Used by the Java run-time system to handle errors occurring in the run-time environment
– Generally beyond the control of user programs
– Examples
- Out of memory errors
- Hard disk crash
Checked and Unchecked Exceptions
● Checked exception
- – Java compiler checks if the program either catches or lists the occurring checked exception
- – If not, compiler error will occur
● Unchecked exceptions
- – Not subject to compile-time checking for exception handling
- – Built-in unchecked exception classes
- Error
- RuntimeException
- Their subclasses
- – Handling all these exceptions may make the program cluttered and may become a nuisance
Creating Your Own Exception Class
● Steps to follow
- – Create a class that extends the RuntimeException or the Exception class
- – Customize the class
- Members and constructors may be added to the class
What are Assertions?
● Allow the programmer to find out if an assumption was met
- – Example: month
● Extension of comments wherein the assert statement informs the person reading the code that a particular condition should always be satisfied
- – Running the program informs you if assertions made are true or not
- – If an assertion is not true, an AssertionError is thrown
● User has the option to turn it off or on at runtime
No comments:
Post a Comment