Hello guys,
>> chalo EXCEPTION ke baare mai baat karte hain
>> ye concept kisi bhi object oriented programming language ke best concepts mai se ek mana jata hai
>> so, exception ka matlab hota hai runtime error, bole toh ek aesi error jo during program execution aa sakti hai, or jiske vajah se program ka execution ruk jata hai
>> in english, exception is an error that may arise during program execution and by that the execution of program will be terminated
>>in java exception are objcets and they are denoted by Throwable class and its subclasses
>> lets have a look on Throwable and its subclasses
Throwable
** inherited by **
Exception and Error
Exception
** inherited by **
RuntimeException
and many more classes
RuntimeException
** inherited by **
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
NegativeArraySizeException
and many more classes
Error
** inherited by **
OutOfMemoryError
VerifyError
and many more classes
>> bole toh, java mai exception ko denote karne ke liye Throwable class hoti hai and iske bahut sare babies (bole toh subclasses) bhi hote hain.
>> agar hum non technically baat karen to yeh man sakte hain ki hamari kisi galti (mistake) ki vajah se exception aati hai, yani exception ek tarha se hamare liye ek punishment hai from the JVM.
>> galti ham karte hai, punishment JVM deta hai
>> for example ...........................
** hamne int value ko zero se divide karne ki galti ki toh JVM hame punishment dega jisko hum ArithmeticException kehte hain
>> example int a = 10 / 0;
** hamne array ka size zero se kam dene ki galti ki toh JVM hame punishment dega jiske hum NegativeArraySizeException kehte hain
>> example int[] myArray = new int[-1];
** hamne kisi non-static-method ko ya non-static-variable ko ek aese reference-var par call karne ki galti ki jisme null hai toh JVM hame punishment dega jisko hum NullPointerException kehte hain
>> example
class Dog
{
public void bark()
{
}
psvm()
{
Dog d = null;
d.bark();
}
}
** hamne kisi aesi string ko int mai ya float mai convert karne ki galti ki jo string int ya float type mai nahin hai toh JVM hame punishment dega jise hum NumberFormatException kehte hain
>> example int i = Integer.parseInt("abcd");
or
float f = Float.parseFloat("abc");
** hamne kisi array ki index zero (bole toh first index) se pehle ya last index (bole toh size of array minus 1) ke baad array ko access karne ki galti ki toh JVM punishment dega jisko hum ArrayIndexOutOfBoundsException kehte hain
>> aesi bahut sari galtiyan hum developer karte hain jinki punishment hame JVM se milti hai, or hamari application crash ho jati hai.
>> so apni application ko crash hone se bachane ke liye hum exception handling kar sakte hai.
>> exception handling ke liye hum 3 keywords use karte hain [TRY/CATCH/FINALLY]
>> basically ye teeno blocks ki tarha use kiye jate hain, inka syntax neeche diya hai maine
>> syntax for TRY/CATCH/FINALLY
try
{
** yahan hum ek aesa code likhte hain jisme se exception aane ke chances hote hain.
aese code ko RISKY CODE kaha jata hai.
}
catch(<Throwable ya iski koyi subclasses ka name> refvar)
{
** yahan woh code likha jata hai jo exception aane ke baad execute karna ho
}
finally
{
** yahan woh code likhte hain joki hamesha chale, chahe exception aaye ya na aaye, finally ka code hamesha execute hota hai
}
>> exception can be classified into checked-exception and unchecked-exception
>> the RuntimeException and their subclasses also the Error and its subclasses are known as unchecked-exception
>> Throwable, Exception and its subclasses except those who belongs to RuntimeException and Error are checked-exception
>> unchecked-exception woh hoti hain jinke bare mai compiler predict nahin kar sakta
>> checked-exception woh hoti hain jinko compiler predict kar sakte hai or aesi exception ke liye hum ek guideline use karte hain jise HANDLE-OR-DECLARE kaha jaata hai. matlab agar kisi code mai checked-exception aa rahi hai toh hame usai HANDLE karna padega by using TRY-CATCH block ya phir usai THROWS keyword ka use karke DECLARE karna padega.
>> example
void main()
{
// NOTE:
// PLEASE PAY ATTENTION
// line no 126 wala code ek risky code
// hai, and ek checked-exception bhi hai
// kyunki compiler janta hai ki agar hamne
// class ka naam wrong likh diya toh
// exception aa jayegi
// ab hame is code ko ya toh try-catch mai rakhna padega
// ya
// phit apne method mai same exception ko declare karna padega using throws keyword
Class.forName("class name");
}
>> example of handling
void main()
{
try
{
Class.forName("class name");
}
catch(Exception e)
{
// NOTE:
// PAY ATTENTION
// printStackTrace() ek method hai
// Throwable class ka joki hame ye bata deta hai ki application mai kya exception aa rahi hai and kon si line par exception aayi hai
e.printStackTrace();
}
}
>> example of declare
void main()
throws Exception
{
Class.forName("class name");
}
NOTE :
PAY ATTENTION >> agar line 152 par koyi exception aayi toh application crash ho jayegi
>> chalo EXCEPTION ke baare mai baat karte hain
>> ye concept kisi bhi object oriented programming language ke best concepts mai se ek mana jata hai
>> so, exception ka matlab hota hai runtime error, bole toh ek aesi error jo during program execution aa sakti hai, or jiske vajah se program ka execution ruk jata hai
>> in english, exception is an error that may arise during program execution and by that the execution of program will be terminated
>>in java exception are objcets and they are denoted by Throwable class and its subclasses
>> lets have a look on Throwable and its subclasses
Throwable
** inherited by **
Exception and Error
Exception
** inherited by **
RuntimeException
and many more classes
RuntimeException
** inherited by **
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
NegativeArraySizeException
and many more classes
Error
** inherited by **
OutOfMemoryError
VerifyError
and many more classes
>> bole toh, java mai exception ko denote karne ke liye Throwable class hoti hai and iske bahut sare babies (bole toh subclasses) bhi hote hain.
>> agar hum non technically baat karen to yeh man sakte hain ki hamari kisi galti (mistake) ki vajah se exception aati hai, yani exception ek tarha se hamare liye ek punishment hai from the JVM.
>> galti ham karte hai, punishment JVM deta hai
>> for example ...........................
** hamne int value ko zero se divide karne ki galti ki toh JVM hame punishment dega jisko hum ArithmeticException kehte hain
>> example int a = 10 / 0;
** hamne array ka size zero se kam dene ki galti ki toh JVM hame punishment dega jiske hum NegativeArraySizeException kehte hain
>> example int[] myArray = new int[-1];
** hamne kisi non-static-method ko ya non-static-variable ko ek aese reference-var par call karne ki galti ki jisme null hai toh JVM hame punishment dega jisko hum NullPointerException kehte hain
>> example
class Dog
{
public void bark()
{
}
psvm()
{
Dog d = null;
d.bark();
}
}
** hamne kisi aesi string ko int mai ya float mai convert karne ki galti ki jo string int ya float type mai nahin hai toh JVM hame punishment dega jise hum NumberFormatException kehte hain
>> example int i = Integer.parseInt("abcd");
or
float f = Float.parseFloat("abc");
** hamne kisi array ki index zero (bole toh first index) se pehle ya last index (bole toh size of array minus 1) ke baad array ko access karne ki galti ki toh JVM punishment dega jisko hum ArrayIndexOutOfBoundsException kehte hain
>> aesi bahut sari galtiyan hum developer karte hain jinki punishment hame JVM se milti hai, or hamari application crash ho jati hai.
>> so apni application ko crash hone se bachane ke liye hum exception handling kar sakte hai.
>> exception handling ke liye hum 3 keywords use karte hain [TRY/CATCH/FINALLY]
>> basically ye teeno blocks ki tarha use kiye jate hain, inka syntax neeche diya hai maine
>> syntax for TRY/CATCH/FINALLY
try
{
** yahan hum ek aesa code likhte hain jisme se exception aane ke chances hote hain.
aese code ko RISKY CODE kaha jata hai.
}
catch(<Throwable ya iski koyi subclasses ka name> refvar)
{
** yahan woh code likha jata hai jo exception aane ke baad execute karna ho
}
finally
{
** yahan woh code likhte hain joki hamesha chale, chahe exception aaye ya na aaye, finally ka code hamesha execute hota hai
}
>> exception can be classified into checked-exception and unchecked-exception
>> the RuntimeException and their subclasses also the Error and its subclasses are known as unchecked-exception
>> Throwable, Exception and its subclasses except those who belongs to RuntimeException and Error are checked-exception
>> unchecked-exception woh hoti hain jinke bare mai compiler predict nahin kar sakta
>> checked-exception woh hoti hain jinko compiler predict kar sakte hai or aesi exception ke liye hum ek guideline use karte hain jise HANDLE-OR-DECLARE kaha jaata hai. matlab agar kisi code mai checked-exception aa rahi hai toh hame usai HANDLE karna padega by using TRY-CATCH block ya phir usai THROWS keyword ka use karke DECLARE karna padega.
>> example
void main()
{
// NOTE:
// PLEASE PAY ATTENTION
// line no 126 wala code ek risky code
// hai, and ek checked-exception bhi hai
// kyunki compiler janta hai ki agar hamne
// class ka naam wrong likh diya toh
// exception aa jayegi
// ab hame is code ko ya toh try-catch mai rakhna padega
// ya
// phit apne method mai same exception ko declare karna padega using throws keyword
Class.forName("class name");
}
>> example of handling
void main()
{
try
{
Class.forName("class name");
}
catch(Exception e)
{
// NOTE:
// PAY ATTENTION
// printStackTrace() ek method hai
// Throwable class ka joki hame ye bata deta hai ki application mai kya exception aa rahi hai and kon si line par exception aayi hai
e.printStackTrace();
}
}
>> example of declare
void main()
throws Exception
{
Class.forName("class name");
}
NOTE :
PAY ATTENTION >> agar line 152 par koyi exception aayi toh application crash ho jayegi