Java SE(Core java)
Lecture-8
Today’s Agenda
01 Creating programmer defined exception\Custom
exception
02 Using the keyword “finally”
03 Multi catch feature of Java
05
Programmer defined\Customized Exceptions
• Many times, in some situations a programmer might not find any predefined exception
class to be used with throw.
For example, in case of a banking application the method withdraw has some minimum
limit like 500 else an exception will generate. In this case there is no predefined java’s
exception class.
• So, Java advises us to design our own exception [Link] classes are called
customized exception class.
• To create an exception class we need to follow some steps,which are as follows :-
Programmer defined\Customized Exceptions
1. Inherit or extend any of the predefined exception class in our own exception class.
2. Provide a parameterized constructor so that exception message can be set and
passed on to parent class’s constructor.
Example
import [Link].*;
class InvalidNumeratorException extends Exception
{
public InvalidNumeratorException(String msg)
{
super(msg);
}
}
class Test
{
public static void main(String [] args)
{
Scanner kb=new Scanner([Link]);
[Link](“Enter two numbers”);
Example
try
{
int a=[Link]();
int b=[Link]();
if(a<=0)
{
throw new InvalidNumeratorException(“Numerator should be
positive”);
}
int c=a/b;
[Link](“Division is ”+c);
}
Example
catch(ArithmeticException ex)
{
[Link]([Link]());
}
catch(InvalidNumeratorException ex)
{
[Link]([Link]());
}
catch(InputMismatchException ex)
{
[Link](“Please input di-
gits only”);
}
}
}
Programmer defined\Customized Exceptions
• The customized exceptions become checked exception if we inherit the base class
Exception.
• Since, only RuntimeException and its child classes are unchecked in nature, so
programmer has to specifically inherit anyone of those to create and unchecked
exception class.
Using the keyword “finally”
• There are certain statements in our program whose execution is so crucial that before
our program gets terminated, these statements must be executed.
• Example, if we have opened a file or any database connection and before the program
completes its execution the file or database connection should be closed.
• In such cases java suggests us to write such statements in a block whose execution is
guaranteed by java and such blocks are created using the keyword finally.
Using the keyword “finally”
• If no exception occurs finally block is executed.
• If an exception occurs inside the try block and its catch hasbeen defined, in such case
also finally is executed.
• Even if no catch block is used then also the finally block is executed.
• Moreover, a try block is required for an finally block. If an exception occurs outside try
block in that case finally block is not executed and also when the method [Link]()
is used.
Syntax
try
try
{
{
---
---
---
}
}
catch( ---)
finally
----
{
}
----
finally
----
----
}
}
}
Example
import [Link].*;
class TestFinally
{
public static void main(String [] args)
{
Scanner kb=new Scanner([Link]);
[Link](“Enter two numbers”);
try
{
int a=[Link]();
int b=[Link]();
int c=a/b;
[Link](“Division is ”+c);
}
Example
catch(ArithmeticException ex)
{
[Link](“Denominator should not be 0”);
}
finally
{
[Link](“Thank you! Have a good day”);
}
}
Multi catch feature
• We can use a single catch to handle multiple exceptions.
• This feature was introduced in java from 7th version.
• Syntax :-
catch(ArithmeticException | InvalidNumeratorException ex)
{
[Link]([Link]);
}
Thank you