CO_IF_22412_UO2
Sushama Pawar, Lecturer, Vidyalankar Polytechnic
Date: 04 February 2021
MSBTE LEAD: Learning at your Doorstep
Unit 04 :
Exception Handling and
Multithreading
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Unit Outcome 2 :
Develop program for handling the
given exception.
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Learning Outcome 2d : Students
should able to create own exception
and understand the concept of
exception in subclass.
Written by
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
What we will learn today
1. How to create own exception? Key takeaways
2. Concept of exception in subclass? Concept of creating own exception and exception in subclass
.
Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Page 5 Maharashtra State Board of Technical Education 2 Feb 2021
Concept Map
Creating own
exception
Exception in
subclass
Page 6 Maharashtra State Board of Technical Education 2 Feb 2021
Learning Objective/ Key learning
► Creating own exception
► Concept of exception in subclass
Page 7 Maharashtra State Board of Technical Education 2 Feb 2021
Concept Explanation: Creating Own Exception/User Define Exception
Java provides us facility to create our own exceptions which are basically derived classes of Exception.
Sometimes, the built-in exceptions in Java are not able to describe a certain situation. In such cases, a
user can also create exceptions which are called ‘User-Defined Exceptions’.
User Defined Exception or custom exception is creating your own exception class and throws that
exception using ‘throw’ keyword. This can be done by extending the class Exception.
Key points
A user-defined exception must extend Exception class.
The exception is thrown using throw keyword.
Page 8 Maharashtra State Board of Technical Education 2 Feb 2021
Example 1: Creating our own exception.
class NegativeOutputException extends Exception System.out.println("Output: "+z);
{ }
private int det; catch (NegativeOutputException e)
NegativeOutputException(int a) {
{ System.out.println("Caught: "+e);
det = a; }
} }
public String toString() }
{
return "NegativeOutputException["+det+"]";
}
}
class OwnException
{
public static void main(String args[])
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);; 9
int z;
try
{
z = x * y;
if(z<0) //statement1
throw new NegativeOutputException(z);
Page 9 Maharashtra State Board of Technical Education 2 Feb 2021
Example 2: Define an exception called “No match Exception‟ that is thrown when a string is not equal
to “MSBTE”.
import java.io.*; else
class NoMatchException extends Exception System.out.println("Strings are equal");
{ }
NoMatchException(String s) catch(NoMatchException e)
{ {
super(s); System.out.println(e.getMessage());
} }
} }
class test1 }
{
public static void main(String args[]) throws IOException
{
BufferedReader br= new BufferedReader(new
InputStreamReader(System.in) );
System.out.println("Enter a word:"); String str= br.readLine();
try 10
{
if (str.compareTo("MSBTE")!=0) // can be done with
equals()
throw new NoMatchException("Strings are not equal");
Page 10 Maharashtra State Board of Technical Education 2 Feb 2021
Example 2: Write a program to input name and age of person and throws user defined exception, if
entered age is negative
import java.io.*; throw new Negative("age is negative");
class Negative extends Exception else
{ System.out.println("age is positive");
Negative(String msg) }
{ }
super(msg); catch(Negative n)
} {
} System.out.println(n);
class Negativedemo }
{ catch(Exception e)
public static void main(String ar[]) {
{ }
int age=0; String name; }
BufferedReaderbr=new BufferedReader (new }
InputStreamReader(System.in)); System.out.println("enter age and
name of person");
try
{
age=Integer.parseInt(br.readLine());
name=br.readLine();
{
if(age<0)
Page 11 Maharashtra State Board of Technical Education 2 Feb 2021
Exception in Subclass/ Exception Handling with Method Overriding
When Exception handling is involved with Method overriding, ambiguity occurs. The compiler gets
confused as which definition is to be followed. Such problems were of two types:
► If the superclass method does not declare an exception
► If the superclass method does not declare an exception, subclass overridden method cannot declare the
checked exception but it can declare unchecked exception.
► If the superclass method declares an exception
► If the superclass method declares an exception, subclass overridden method can declare same, subclass
exception or no exception but cannot declare parent exception.
Page 12 Maharashtra State Board of Technical Education 2 Feb 2021
If the superclass method does not declare an exception
import java.io.*; public static void main(String args[])
{
class SuperClass { SuperClass s = new SubClass();
s.method();
// SuperClass doesn't declare any exception }
void method() }
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// method() declaring Checked Exception IOException
void method() throws IOException
{
// IOException is of type Checked Exception
// so the compiler will give Error
System.out.println("SubClass");
}
Page 13 Maharashtra State Board of Technical Education 2 Feb 2021
If the superclass method declares an exception
import java.io.*; public static void main(String args[])
{
class SuperClass { SuperClass s = new SubClass();
s.method();
// SuperClass declares an exception }
void method() throws RuntimeException }
{
System.out.println("SuperClass");
}
}
// SuperClass inherited by the SubClass
class SubClass extends SuperClass {
// SubClass declaring a child exception
// of RuntimeException
void method() throws ArithmeticException
{
// ArithmeticException is a child exception
// of the RuntimeException
// So the compiler won't give an error
System.out.println("SubClass");
}
Page 14 Maharashtra State Board of Technical Education 2 Feb 2021