JAVA - Interfaces | How to use | Example - Java2021 : Online Tutorials Hub

Java2021 : Online Tutorials Hub

JAVA | Advance Java | MySQL | Data Structure | Git | HTML | CSS | JS

Latest

Post Top Ad

Thursday, April 23, 2020

JAVA - Interfaces | How to use | Example

Exception Handling


The Exception Handling in Java is one of the powerful mechanism to handle
the runtime errors so that normal flow of the application can be maintained.

other means--Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.

--It is an object which is thrown at runtime.

Types of Java Exceptions

-Checked Exception
-Unchecked Exception

 Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error are known
as checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-time.

Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

other keywords :
>>>try :: The "try" keyword is used to specify a block where we should place exception code.
The try block must be followed by either catch or finally. It means, we can't use try block alone.

>>>catch :: The "catch" block is used to handle the exception.
--It must be preceded by try block which means we can't use catch block alone.
--It can be followed by finally block later.

>>>finally block
-Java finally block is a block that is used to execute important code such as closing connection, stream etc.
-Java finally block is always executed whether exception is handled or not.
-Java finally block follows try or catch block.

Why use java finally
Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.

>>>throw :: The "throw" keyword is used to throw an exception.

>>>throws :: The "throws" keyword is used to declare exceptions.
--It doesn't throw an exception.
--It specifies that there may occur an exception in the method.
--It is always used with method signature.

- Methods of Exception class


public String getMessage( ) : Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
public Throwable getCause( ) : Returns the cause of the exception as represented by a Throwable object.
public String toString( ) : Returns the name of the class concatenated with the result of getMessage().

No comments:

Pages