You are on page 1of 31

Exceptions

Java SE
Contents
• About Exception

• Call Task

• Try – Catch – Throw

• Type of Exceptions

• Catching and Handling Exceptions

• Throwing Exception

• Creating Exception

• Chaining Exceptions

• Benefits of Exceptions
About Exception
• Java ဘ"သ"ရပ&တ(င& Program အတ(င&+ eမ."&လင&0မထ"+eသ" အမ2"+မ3"+အ"+
Exception မ3"+အeနန2င&0 eဖ"&6ပeလ07i၏

• Java Environment အတ(င&+ Method Invocation အတ(င&+ အမ2"+6ဖစ&eပ;ပ<


က Exception Object အ"+ Create လuပ&၍ Runtime System ဆAသiu 6ပန&ပiu
eပ+ပ<သည&

• Exception Object အတ(င&+ အမ2"+6ဖစ&eပ;eစeသ" Program ၏ အမ3iu+အစ"+


E2င&0 အeနအထ"+တiuအ"+ eဖ"&6ပEiuင&eသ" သတင&+အခ3က&အလက&မ3"+ ပ<ဝင&
ပ<သည&

• ဤကI0သiu Exception အ"+ Runtime System ဆAသiu 6ပန&ပiu6ခင&+အ"+


Exception အ"+ Throw လuပ&သည&ဟu eခ;ပ<သည&
Call Task
• Runtime System သည& Exception Object အ"+ ရ7iKပA+eန"က&
Method Invocation လမ&+eLက"င&+တစ&eလ."က& Exception အ"+
Handle လuပ&Eiuင&eသ" Handler အ"+ 7"eဖ(ပ<သည&

• Exception အ"+ Handle လuပ&Eiuင&eသ" Code Block အ"+ eတ(07i


ပ<က Exception အ"+ Handler ဆAသiuလMIe6ပ"င&+eပ+ပ<မည&။ ဤ
သည&ကiu Exception အ"+ Catch လuပ&သည&ဟu eခ;ဆiuပ<သည&

• အကယ&၍ Invocation လမ&+တစ&eလ."က& Main Method အထi


Exception Handler အ"+ မeတ(07iပ<က System အ"+ Error 6ဖင&0
အဆuP+သတ&eစမည& 6ဖစ&ပ<သည&
Call Task
Try Catch Statement
• Method Invocation မ3"+တ(င& 6ဖစ&ပ("+eသ" Exception မ3"+အ"+ Handle
လuပ&လiuပ<က try-catch statement 6ဖင&0 eရ+သ"+Eiuင&ပ<သည&

• Catch Block တ(င& Handle လuပ&လiuသQ&0 Exception အ"+ Parameter


အ6ဖစ& eရ+သ"+ထ"+ပ<သည&

• Try Block အတ(င&+7i Method Invocation မ3"+တ(င& Catch လuပ&မQ&0


Exception အမ3"+6ဖစ&ပ("+ပ<က Catch Block အတ(င&+7i ကuဒ&မ3"+6ဖင&0 Handle
လuပ&Eiuင&မည& 6ဖစ&သည&

• အကယ&၍ Method အတ(င&+ Exception အ"+ Handle မလuပ&လiuပ<က


Method တ(င& throws keyword အ"+ အသuP+6ပu၍ Exception အ"+ Throw
လuပ&Eiuင&ပ<သည&
Sample
public void doUpdate(Employee e) {
try { Codes which can be
em.getTransaction().begin(); cause Exception
em.persist(e);
em.getTransaction().commit();
} catch (SQLException ex) {
logger.severe(e.getMessage()); Codes that can control
exception
em.getTransaction().rollback();
}
}
Throwing Exception
public void doUpdate(Employee e) throws SQLException{
em.getTransaction().begin();
em.persist(e);
em.getTransaction().commit();
}

• အကယ&၍ 6ဖစ&ပ("+လ"eသ" Exception အ"+ လက&7i method


အတ(င&+တ(င& Control မလuပ&လiuပ<က method declaration တ(င&
Exception အ"+ throw လuပ&က" eရ+Eiuင&ပ<သည&

• 6ဖစ&eပ;လ"eသ" Exception အ"+ အဆiuပ< method ကiu invoke


လuပ&eသ" eနရ"တ(င& Control လuပ&ရန&လiuအပ&ပ<သည&
Type of Exceptions
• Java ဘ"သ"ရပ&တ(င& Exception မ3"+အ"+ ၃မ3iu+ခ(I6ခ"+Eiuင&ပ<သည&
Checked Exception
• အeရ+မ3"+eသ" အမ2"+မ3"+အ"+ 6ပန&လည&စမ&+စစ&၍ 6ပu6ပင&
Eiuင&ရန& Checked Exception ဟu သတ&မ2တ&

• Checked Excpetion မ3"+အ"+ မ6ဖစ&မeန Catch လuပ&၍


Handle လuပ&ရန& လiuအပ&

• Java Environment တ(င& Error E2င&0 Runtime Exception


မ3"+၏ Subclass မ3"+မ2လ(I၍ က3န&eသ" Exception
မ3"+သည& Checked Exception မ3"+ 6ဖစ&Lကသည&
Error
• Application E2င&0 မဆiuင&eသ" အeLက"င&+အရင&+eLက"င&0
6ဖစ&ပ("+eသ" Exception မ3"+6ဖစ&KပA+၊ Application မ3"+တ(င&
Handle မလuပ&Eiuင&eသ" အမ2"+မ3"+အ"+ Error အမ3iu+အစ"+
ဟu သတ&မ2တ&ထ"+ပ<သည&

• Error မ3"+အ"+ Error Class E2င&0 ၎င&+၏ Subclass မ3"+6ဖင&0


ကiuယ&စ"+6ပuပ<သည&

• Application အတ(င&+ Error Exception 6ဖစ&ပ("+ပ<က ၎င&+


အ"+ Catch လuပ&၍ အသuP+6ပuသVအ"+ အသieပ+သင&0ပ<သည&
Runtime Exception
• Application eLက"င&0 6ဖစ&eပ;လ"eသ" အမ2"+မ3"+အ"+ Runtime
Exception ဟu သတ&မ2တ&ပ<သည&

• Program Bug eLက"င&0eသ"&၎င&+၊ Logic မ2"+ယ(င&+မW0eLက"င&0eသ"&၎င&+၊ မ


သင&0eတ"&eသ" API အ"+ အသuP+6ပuမieသ"&၎င&+ Runtime Exception
အ"+ 6ဖစ&ပ("+eစEiuင&

• Runtime Exception အ"+ Application အတ(င&+ Catch လuပ&၍ Bug


မ3"+အ"+ 6ပu6ပင&ရ"တ(င& အeထ"က&အကV 6ပueစEiuင&

• Runtime Exception အ"+ RuntimeException E2င&0 ၎င&+၏ Subclass


မ3"+E2င&0 ကiuယ&စ"+6ပu
Catching and Handling
Exceptions

• Java ဘ"သ"ရပ&တ(င& Exception မ3"+အ"+ Catch လuပ&၍


Handle လuပ&လiuသQ&0အခ< အe6ခခPအ"+6ဖင&0 try – catch –
finally statement အ"+ အသuP+6ပuပ<သည&

• Java SE 7 အeရ"က&တ(င& try with resources statement


ကiuလည&+ အသuP+6ပuEiuင&ပ<သည&
Try - Catch - Finally
try {
// method invocations

} catch (Exception e) {
// exception handle codes

} finally {
// finally processing codes

}
Try Block
• Exception အ"+ Handle လuပ&ရန& ပထမဦ+ဆuP+အeနန2င&0
Exception 6ဖစ&eစEiuင&eသ" Code မ3"+အ"+ try block E2င&0
ဝန&+ရP၍ eရ+သ"+ရမည&

• အကယ&၍ try block အတ(င&+ eရ+သ"+ထ"+eသ"


Statement အတ(င&+ Exception 6ဖစ&ပ("+ပ<က၊ ၎င&+၏
eန"က&တ(င& eရ+သ"+ထ"+eသ" catch လuပ&ထ"+eသ"
Exception အလiuက& Handle လuပ&သ("+ပ<မည&
Catch Block
• Try Block အတ(င&+ 6ဖစ&ပ("+eသ" Exception မ3"+အ"+ e6ဖ7င&+eပ+Eiuင&eသ"
Exception Handle အစiတ&အပiuင&+မ3"+ 6ဖစ&သည&

• Try Block တစ&ခuအ"+ Catch Block တစ&ခu သiuမဟuတ& အမ3"+E2င&0 တ(Iဖက& eရ+သ"+
Eiuင&ပ<သည&

• 6ဖစ&eပ;လ"eသ" Exception အမ3iu+အစ"+အလiuက& Catch လuပ&၍ Catch Block


အတ(င&+7i ကuဒ&မ3"+E2င&0 Handle လuပ&သ("+မည&6ဖစ&သည&

• Java SE 7 အeရ"က&တ(င& catch block အတ(င&+ Handle လuပ&လiuသQ&0


Exception အမ3"+အ"+ တ(I၍eရသ"+Eiuင&သည&

• Catch Block တစ&ခuအတ(င&+ Exception အမ3"+အ"+ eရ+သ"+ ထ"+6ခင&+အ"+ Multi


Catch ဟu eခ;ဆiuသည&
Try with multiple handler
try {
// statement which may thrown exception

} catch (ExceptionType1 name) {


// handling code

} catch (ExceptionType2 name) {


// handling code
}
Multi-Catch
try {
// statement which may thrown exception

} catch (ExceptionType1 ¦ ExceptionType2 name) {


// handling code

}
Finally Block
• Try – catch – finally Statement တ(င& finally block ကiu
လည&+ eရ+သ"+Eiuင&KပA+၊ try block အတ(င&+eရ+သ"+ထ"+
eသ" Statement မ3"+အ"+ eဆ"င&Y(က&KပA+ eန"က&ဆuP+တ(င&
finally block အတ(င&+ eရ+သ"+ထ"+eသ" Statement မ3"+
အ"+ eဆ"င&Y(က&မည& 6ဖစ&သည&

• Finally Block အ"+ try – catch – finally statement ၏


eန"က&ဆuP+တ(င& eရ+သ"+ရပ<မည&
Try with resources
• Java Programming တ(င& အသuP+6ပueနeသ" Database Connection, File I/O
မ3"+ကI0သiu အခ3iueသ" Class မ3"+သည& အသuP+6ပuKပA+eသ"အခ< Close လuပ&ရန&
လiuအပ&ပ<သည&

• ၎င&+ Class မ3"+အ"+ အသuP+6ပuရ"တ(င& အသuP+6ပuKပA+eန"က&ဆuP+eနရ"တ(င&၎င&+၊


Exception အ"+ catch လuပ&သQ&0 Block အတ(င&+တ(င&၎င&+၊ Finally Block
အတ(င&+တ(င&၎င&+၊ close လuပ&သQ&0 Statement မ3"+အ"+ eရ+သ"+ရပ<သည&

• သiuရ"တ(င& Java SE 7 အeရ"က&တ(င& ၎င&+တiuအ"+ eန"က&ဆuP+တ(င& အလiuအ


eလ."က& close လuပ&Eiuင&eသ" eရ+သ"+နည&+ အ"+ အသစ& 6ဖQ&0စ(က&လ"ခI0ပ<သည&

• ဤသiueရ+သ"+ထ"+ပ<က Close လuပ&ရန&လiuအပ&eသ" Resource မ3"+အ"+ အလiu


အeလ."က& Close လuပ&eပ+မည&6ဖစ&သည&
Without using
Try-With-Resource
try {
// connection
Connection conn = this.getConnection();
// business code
} catch (Exception e) {
// codes for handeling
} finally {
try {
conn.close();
} catch (Exception e) {}
}
Using
Try-With-Resource

try (Connection conn = this.getConnection()){


// business code
} catch (Exception e) {
// codes for handeling
}
Throwing Exception

• Java ဘ"သ"ရပ&တ(င& Exception မ3"+အ"+ Handle လuပ&


Eiuင&သလiu၊ လiuအပ&သQ&0 eနရ"တ(င&လည&+ Exception မ3"+
အ"+ 6ဖစ&ပ("+eစEiuင&ပ<သည&

• 6ဖစ&ပ("+eစလiuသQ&0 eနရ"တ(င& E2စ&သက&ရ" Exception


Object အ"+ Create လuပ&၍ throw keyword အ"+
အသuP+6ပuက" Throw လuပ&Eiuင& ပ<သည&
Sample
public void writeList(List<String> lines) {
// if no lines throw exception
if(null == lines != lines.size() == 0) {
throw new RuntimeException( No data to print );
}
// print lines
for(String s : lines) {
System.out.println(s);
}
}
Custom Exception
public class NeedToAlertException extends RuntimeException {
public NeedToAlertException(String message) {
super(message);
}
}

• Exception အ"+ အသuP+6ပuရ"တ(င& Java SE ၏ API တ(င&ပ<ဝင&eသ" Exception Class မ3"+


အ"+ အသuP+6ပuEiuင&သလiu ကiuယ&ပiuင& Exception Class မ3"+အ"+ eရ+သ"+၍လည&+ အသuP+6ပuEiuင&

• Exception အ6ဖစ& Throw လuပ&Eiuင&ရန& Throwable အ"+ Extends လuပ&ရန& လiuအပ&KပA+၊


Exception Class မ3"+သည& Throwable Class ၏ Subclass မ3"+6ဖစ&Lကသည&

• Custom Exception Class မ3"+သည&လည&+ E2စ&သက&ရ" Exception Class မ3"+အ"+


Extends လuပ&၍ eရ+သ"+Eiuင&ပ<သည&
Using Custom Exception
public void checkInput() {
if(nameInput.getText().isEmpty())
throw new NeedToAlertException( Please Set Name );
if(password.getText().isEmpty())
throw new NeedToAlertException( Please Set Password );
}
public void register() {
try {
this.checkInput();
// other codes
} catch (NeedToAllertException e) {
this.showAllertDialog(e.getMessage());
} catch (Exception e) {
// handle code
}
}
Changing Exception
• Application မ3"+အ"+ eရ+သ"+ရ"တ(င& Exception တစ&ခu 6ဖစ&ပ("+6ခင&+eLက"င&0
အ6ခ"+eသ" Exception အ"+ 6ဖစ&ပ("+eစeသ" အခ<မ3iu+လည&+ 7iသည&။

• ဥပမ"အ"+6ဖင&0 အထက&တ(င&eဖ"&6ပထ"+သQ&0 နမWန"တ(င& Database အ"+


Update လuပ&သQ&0eနရ"တ(င& SQL Exception တစ&ခu 6ဖစ&ပ("+KပA+၊ ၎င&+အ"+
Message Bar တ(င& eဖ"&6ပလiuသQ&0အခ< မ3iu+တ(င& FatalException အeနန2င&0
6ပန&၍Throw လuပ&Eiuင&ပ<သည&။

• ဤကI0သiu Throw လuပ&ရ"တ(င&လည&+ FatalException အ"+ 6ဖစ&ပ("+eစeသ"


အeLက"င&+အရင&+အeနန2င&0 SQLException Object အ"+ သတ&မ2တ&အသuP+6ပu
Eiuင&ပ<သည&။

• ၎င&+အ"+ chaining exception ဟu eခ;ဆiuပ<သည&။


Constructors
Constructor Description

Parameter အ6ဖစ& 6ဖစ&ပ("+eစeသ" Throwable


Throwable(Throwable cause) Object အ"+ ထQ&0သ(င&+ထ"+eသ" Throwable Object
အ"+ တည&eဆ"က&eပ+မည& 6ဖစ&သည&။

Throwable( Parameter အ6ဖစ& Error 6ဖစ&ပ("+eစeသ" Message E2


Strng message, င&0 မWရင&+ Throwable Object အ"+ 6ဖQ&0စ(က&၍ eရ+သ"+
Throwable cause) ရပ<မည&။
Methods
Method Description

Exception အ"+ 6ဖစ&ပ("+eစeသ" Throwable


Throwable getCause()
Object အ"+ ရ7iEiuင&eသ" getter method 6ဖစ&၏။

Exception Object အ"+ 6ဖစ&ပ("+eစeသ"


Throwable initCause(Throwable အeLက"င&+အရ"အ"+ Argument Throwable
cause) Object 6ဖင&0 initialize လuပ&eသ" Method 6ဖစ&
ပ<သည&။
Sample
public void insertData(User user) {
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()){
stmt.executeUpdate(this.getInsertSql(user));
} catch (SQLException e) {
this.logOut(e.getMessage());
throw new FatalException( SQL Exception , e);
}
}
Benefits of Exception

• Error Handling Code မ3"+E2င&0 Business Code မ3"+အ"+


ခ(I6ခ"+၍ eရ+သ"+Eiuင&6ခင&+

• Error မ3"+အ"+ Call Stack တeလ."က& အထက&သiu လက&


ဆင&0ကမ&+ ပiuEiuင&6ခင&+

• Error မ3"+အ"+ အမ3iu+အစ"+ခ(I၍ eရ+သ"+Eiuင&6ခင&+

You might also like