Exceptional Handling

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 53

Exceptional Handling in Java

Unit-5
CO-6 : Analyzing Threads, Exceptional Handling and I/O in Java.
BTL - Applying (L3)
PO: 2, 3, 4, 5, 12
PSO: 2, 3
BY:
Mrs. Padmini Chattu,
Asst. Professor, CSE
Java’s Checked Exceptions Defined in java.lang
Java’s Unchecked RuntimeException Subclasses
{
{
int a,b=4,c=6;
a=b+c;
S.o.p(a);
}
}
Exceptions are the unwanted errors or bugs or events that restrict the normal
execution of a program. Each time an exception occurs, program execution gets
disrupted. An error message is displayed on the screen.
There are several reasons behind the occurrence of exceptions. These are some
conditions where an exception occurs:
 Whenever a user provides invalid data.
 The file requested to be accessed does not exist in the system.
 When the Java Virtual Machine (JVM) runs out of memory.
 Network drops in the middle of communication.
Exception Hierarchy
 The Exception class, it is a subclass of the built-in Throwable class.
 There is another subclass which is derived from the Throwable class i.e. Error
 The error can be defined as an abnormal condition that indicates something has
gone wrong with the execution of the program. These are not handled by Java
programs.
 There are mainly two types of exceptions in Java as follows:
 Checked exception
 Unchecked exception
Checked exception
 Checked exceptions are also known as compile-time exceptions as these exceptions
are checked by the compiler during the compilation process to confirm whether the
exception is handled by the programmer or not. If not, then the system displays a
compilation error.
  For Example, SQLException, IOException, InvocationTargetException, and
ClassNotFoundException
 It is clearly displayed in the output that the program throws exceptions during the
compilation process.
 There are two methods of resolving such issues.
 You can declare the exception with the help of the throw keyword.
 Another way to resolve exceptions. You can manage them with the help of try-
catch blocks.
There are some important methods available in the Throwable class which are as follows:
public String getMessage() – Provides information about the exception that has
occurred through a message, which is initialized in the Throwable constructor.
public Throwable getCause() – Provides root cause of the exception as represented by
a Throwable object.
public void printStackTrace() – Used to display the output of toString() along with
the stack trace to System.err (error output stream).
public StackTraceElement [] getStackTrace() – Returns an array with each element
present on the stack trace. The index 0 element will symbolize the top of the call stack,
and the last element of array will identify the bottom of the call stack.
public Throwable fillInStackTrace() – Fills the stack trace of this Throwable object
with the current stack trace, adding to any previous information in the stack trace.
Common Exceptions
In Java, it is possible to define two catergories of Exceptions and Errors.
JVM Exceptions − These are exceptions/errors that are exclusively or logically
thrown by the JVM. Examples: NullPointerException,
ArrayIndexOutOfBoundsException, ClassCastException.
Programmatic Exceptions − These exceptions are thrown explicitly by the
application or the API programmers. Examples: IllegalArgumentException,
IllegalStateException.
Java try and catch
The try statement allows you to define a block of code to be tested for errors while it
is being executed.
The catch statement allows you to define a block of code to be executed, if an error
occurs in the try block.
The try and catch keywords come in pairs:
Java throws keyword

The Java throws keyword is used to declare an exception. It gives an information


to the programmer that there may occur an exception so it is better for the
programmer to provide the exception handling code so that normal flow can be
maintained.

Syntax of java throws

return_type method_name() throws exception_class_name
{  
//method code  
}  
Difference between throw and throws in Java

No. throw throws


1) Java throw keyword is used to Java throws keyword is used to declare an
explicitly throw an exception. exception.
2) Checked exception cannot be Checked exception can be propagated with
propagated using throw only. throws.
3) Throw is followed by an Throws is followed by class.
instance.
4) Throw is used within the Throws is used with the method signature.
method.
5) You cannot throw multiple You can declare multiple exceptions e.g.
exceptions. public void method() throws
IOException,SQLException.
Difference between final, finally and finalize

No final finally finalize


1) Final is used to apply Finally is used to place Finalize is used to perform
restrictions on class, important code, it will be clean up processing just
method and variable. Final executed whether before object is garbage
class can't be inherited, exception is handled or collected.
final method can't be not.
overridden and final
variable value can't be
changed.

2) Final is a keyword. Finally is a block. Finalize is a method.

You might also like