0% found this document useful (0 votes)
6 views3 pages

Exception & Error

The document explains the difference between errors and exceptions in programming, highlighting that errors are syntax-related issues while exceptions are logical errors in syntactically correct code. It categorizes exceptions into compile-time (checked) and runtime (unchecked) exceptions, detailing various types of each along with examples. Additionally, it covers exception handling mechanisms in Java, including try, catch, finally, and the use of throw and throws for user-defined exceptions and compile-time exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Exception & Error

The document explains the difference between errors and exceptions in programming, highlighting that errors are syntax-related issues while exceptions are logical errors in syntactically correct code. It categorizes exceptions into compile-time (checked) and runtime (unchecked) exceptions, detailing various types of each along with examples. Additionally, it covers exception handling mechanisms in Java, including try, catch, finally, and the use of throw and throws for user-defined exceptions and compile-time exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Exception & Error:

==================
Error - If the program is syntax wise incorrect and if the machine is not
compatible to run the program then it is called as error.
Exception - If the program is syntax wise correct and machine is compatible to run
the program but it is logically incorrect.

Exception:
----------
There are two types:
1) Compile time exception or checked exception
- It is mandatory to declare or handle
- Before executing the program
class A class B
Independent class Dependant class
A a=new A();
B b=new B(); - A
2) RunTime exception or unchecked exception
- It is not mandatory to declare or handle
- After executing the program

RunTime exception:
------------------
i) Arithmetic exception
- Any number divided by zero will leads to infinity
- Infinity cannot be handled by java so we will get arithmetic
exception
ii) Nullpointer exception
- TreeSet(if null value is added)
- If an object is declared null - if we perform any operation
over the object
iii) IndexOutOfBoundsException
- When index exceeds the size or length(List)
iv) StringIndexOutOfBoundsException
- When index exceeds the size or length(String)
v) ArrayIndexOutOfBoundsException
- When index exceeds the size or length(Array)
vi) Number format exception
- Source and target are not in same format
vii) InputMismatchException
- When the input is not matching with the scanner class method
viii) ClassCastException
- In TreeSet - Different datatypes

Compile time exception:


----------------------
Object - super most class
String - sub class of Object

i) FileNotFoundException
- File related operations
- Reading the data from the text file
- Step 1: create a text file and store the file inside the
src/test/resources folder
- Step 2: Download and add a jar file called as [Link] jar file
jar - java archieve or external library
google - download [Link] jar file 2.7 - select the
first link - jar option - click
jar file will be downloaded
- Step 3: Copy and paste the downloaded file inside the resources
folder
- Step 4: Integrate the commons io jar file with java library file
right click on the commons io jar file - build path - add
to build path - click
- Step 5: Create object for pre defined class called as file
File f=new File("path of the date file");
How to take path - right click on the file - properties -
copy the location
- Step 6: To read the data from the text file
Syntax: [Link](f);
ii) IOException
- Super class of FileNotFoundException
iii) ClassNotFoundException
- Oracle - DB - Read the data from the database using java(JDBC
connection)
- JDBC - Java DataBase Connection
- Step 1: Load the database driver
Syntax: [Link]("oracle");
Class - pre defined class present in java
forName() - static method
[Link](); - static method
Load the database driver - ClassNotFoundException
iv) SQLException
- Step 2: Enter the username and password for database
Syntax:
[Link]("url","username","password");

Exception Handling:
===================

try - It is used to throw the error


catch - It is used to handle the error
finally - It will execute the business logic if the exception is handled or not
handled

Different combination of try,catch and finally


-----------------------------------------------

try - catch------possible
try - n catch------possible
try - catch - finally------possible
try - n catch - finally------possible
try - not possible
try - finally------possible
finally - not possible

Multiple catch blocks - sub class-->super class - possible


- super class-->sub class - not possible

throw, throws
=============
throw
-----
User defined exception class - A class which extends Exception
ex , String - length - >5 valid string,exception

Throw Throws

======
======
It is used to declare the user defined exception class It is
used to handle the compile time exception
Declared inside a method Block Declared
in the method Signature
handle only one Exception handles Multiple
Exception

You might also like