Exception Handling and Super

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

EXCEPTION HANDLING

In Java, an exception is an event that disrupts the normal flow of the prog ram.
It is an object which is thrown at runtime.

There are several reasons behind the occurrence of exceptions. Some cases like

 Whenever a user provides invalid data.


 The file requested to be accessed does not exist in the system.

Exception Handling is mechanism to handle the runtime errors so that normal


flow of the application can be maintained.
In languages where exception handling is not supported errors must be handled
manually using error codes and so on. But javas exception handling avoids these
problems and provide run time error management.
An exception disrupts the normal flow of the application that is why we use
exception handling
Suppose like if we take 5 statements and exception occurs at statement 2 then
statement 3,4,5 will not execute. In this case by performing exception handling
we can solve the issue.
Try, catch, finally, throw, throws are the 5 keywords which we use in exception
handling.
The try block contains set of statements where an exception can occur. A try
block is always followed by a catch block, which handles the exception that occurs
in associated try block.
And to throw exception manually we use throw keyword.
Throws keyword is used to declare exceptions. It specifies that there may occur
an exception in the method.
Finally block is used to execute the code after try and catch block. Whether the
exception is handled or not finally block is always gonna execute.
In types of exception class Throwable is an in-built class which is at the top of the
exception class hierarchy
With two subclasses exception and error.
Exception class is used for exceptional conditions that user program should catch.
Important subclass of exception class is runtime exception.it is an exception which
occurs during run time.
The error can be defined as an abnormal condition that indicates something has
gone wrong with the execution of the program.
SUPER()

Super keyword in Java is a reference variable that refers to an


immediate superclass object. It can be applied to variables, methods, and
constructors of superclass. It allows users to access members of a superclass in a
subclass.
 One use of super is to call superclass constructor.
Like subclass constructor calling superclass constructor.
In inheritance constructors are not inherited. So we need to call them explicitly
and we can do that by using the super keyword.
super() can be used only inside the subclass constructor and must be the first
statement. and if it’s not the first statement then it’s gonna give error
If a Super class have parameterized constructor. You need to accept these
parameters in the sub class’s constructor and within it, you need to invoke the
super class’s constructor using “super()”
The parameters in the super call must match the order and type of the instance
variable declared in the superclass.
 Another use of super is to access superclass members that has been hidden
by subclass member.
Super.member is the general form for this usage.
Here member can either be a method or an instance variable.
For eg assume class A has variable x and class B which is a subclass of A also has
variable x .in this case variable x in B hides the x in A. So here we can use
super.x=a in class B to access the x from class A.
Super can also be used to call methods that are hidden by a subclass.

You might also like