Professional Documents
Culture Documents
Pre-assessment Questions
1. Which type of inheritance supports repeating inheritance?
a. Single level inheritance
b. Multiple inheritance
c. Multilevel inheritance
d. Multipath inheritance
Solutions to Pre-assessment
Questions
1. b. Multiple inheritance
2. d. final
3. a. Polymorphism
4. d. public
5. b. Multiple inheritance
Objectives
In this lesson, you will learn to:
Identify the various types of exceptions in Java
Handle exceptions by using the try, catch, and finally clauses
Use the throw statement
Implement user-defined exceptions
Use assertions in Java
Exceptions in Java
An exception can be defined as an abnormal event that occurs during program
execution and disrupts the normal flow of instructions.
Errors in a Java program are categorized into two groups:
Compile-time errors
Run Time errors
Concept of Exceptions:
The unexpected situations that occur during program execution are:
Running out of memory
Resource allocation errors
Inability to find files
Problems in network connectivity
try
{
// Block of code
}
finally
{
// Block of code that is always executed irrespective of
an exception being raised or not.
}
Throwing an Exception
Using the throw statement
The throw statement causes termination of the normal flow of control of the
Java code and stops the execution of the subsequent statements if an exception
is thrown when the throw statement is executed.
The throw clause transfers the control to the nearest catch block handling the
type of exception object throws.
The following syntax shows how to declare the throw statement:
throw ThrowableObj
Using the throws statement
The throws statement is used by a method to specify the types of exceptions
the method throws.
If a method is capable of raising an exception that it does not handle, the
method must specify that the exception has to be handled by the calling
method.
This is done using the throws statement.
Methods Description
Demonstration-Exception Handling in
Java
Problem Statement
Demonstration-Exception Handling in
Java (Contd.)
Solution
Assertions in Java
Assertions are the checks provided by the Java language to ensure that any
assumption made at the start of a program is true throughout the program.
Assertions:
Assertions are statements in Java that enable you to test the
assumptions made in a program during program execution.
An assertion statement contains a boolean expression that the
programmer believes to be true at the time the statement is executed.
Assertions are used during testing of a program to ensure that the
specified condition is true.
The following syntax shows how to declare an assertion using assert
statement in Java:
assert expressionA;
The following syntax shows the compilation of the Java program named
AssertDemo.java that is using assert statement:
javac -source 1.4 AssertDemo.java
Summary
In this lesson, you learned:
Summary (Contd.)
You can implement exception handling in your program by using the
following keywords:
try
catch
throw
throws
finally
You use multiple catch blocks to throw more than one type of exception.
The finally clause is used to execute the statements that need to be
executed whether or not an exception has been thrown.
The throw statement causes termination of the normal flow of control of
the Java code and stops the execution of subsequent statements after
the throw statement.
The throws clause is used by a method to specify the types of exceptions
the method throws.
You can create your own exception classes to handle the situations
specific to an application
NIIT Programming in Java Lesson 1B / Slide 25 of 25