exception
Embed Size (px)
TRANSCRIPT
Khi nim v Exception
Syntax error (Li c php) Logical error (Li logic) Run-time error (Li thc thi)Syntax error Logical error Run-time error
Thng thng c cc IDE h tr x l
Run-time Error
Chia cho 0 (Divide by zero errors) Truy cp n cc phn t ngoi mng (Accessing the elements of an array beyond its range) D liu nhp vo khng hp l (Invalid Input) Li vt l ca a cng (Hardisk crash)
Truy xut mt tp tin khng tn ti (Opening a non-existent file)Li b nh (Heap memory exhausted)
V dpublic class Bai01 { public static void main(String[] args) { division(100,4); // Line 1 division(100,0); // Line 2 System.out.println("Exit main()."); } public static void division(int totalSum, int totalNumber) { System.out.println("Computing Division."); int average = totalSum/totalNumber; System.out.println("Average : "+ average); } }
Cch x l li truyn thng Lm cho chng trnh tr nn kh hiu Khng phi lc no cng y thng tin x l Khng nht thit phi x l Thng qua tham s, gi tr tr li hoc bin tng th
Ci t on m x l ni pht sinh ra li
Truyn trng thi ln mc trn
D nhm Cn nhiu thiu st
Cc lp trnh vin qun Bn cht ngi lp trnh Thiu kinh nghim, c tnh b qua khng x l l li
public class Bai01 { public static void main(String[] args) { division(100,4);// Line 1 division(100,0); // Line 2 System.out.println("Exit main()."); } public static void division(int totalSum, int totalNumber) { int average=0; System.out.println("Computing Division."); if (totalNumber !=0) average = totalSum/totalNumber; else average=0; System.out.println("Average : "+ average); } }
Exception L nhng i tng c bit c vit sn trong gi java.lang T ng pht sinh trong lc thc thi chng trnh gp li. Mang thng tin nhng li xy ra C mc ch d tm v a ra cc gii php thch hp.
Cc loi ExceptionChecked Exception Unchecked Exception Error
u im ca nm bt ngoi l D dng x l ngoi l D s dng Nm nhiu ngoi l khc nhau Tch m x l S dng c php khc
Tch x l ngoi l khi thut ton
Khng b st li
Lm chng trnh d c v an ton
Ph h ngoi l trong JavaThrowable
Error
Exception
Virtual Machine Error
AWT Error
Runtime Exception
IO Exception
Out of Memory
Stack Overflow
Arithmetic Exception
NullPointer Exception
EOF Exception
FileNotFound Exception
X l ngoi lKhi try/catch t on m c kh nng xy ra ngoi l trong khi try t on m x l ngoi l trong khi catch Khi xy ra ngoi l trong khi try, cc lnh trong khi catch c thc thi. Khi thc hin xong khi catch, iu khin s c tr li cho chng trnh.
C phptry { // on m c kh nng gy ra ngoi l } catch(ExceptionType e1) { // X l ngoi l 1 } catch(ExceptionType eN) { // X l ngoi l th n }
V d x l ngoi lpublic class Bai02 { public static void main(String[] args) { division(100,4); // Line 1 division(100,0); // Line 2 System.out.println("Exit main()."); } public static void division(int totalSum, int totalNumber) { int average=0; System.out.println("Computing Division."); try{ average = totalSum/totalNumber; } catch (Exception e){ System.out.println("Err: "+e); } System.out.println("Average : "+ average); } }
Khi finally Khi mt ngoi l xy ra, chng trnh dng li, mt s cng vic dn dp c th s khng c thc hin Khi finally m bo rng cc cu lnh trong lun c thc hin, k c khi ngoi l xy ra.
C phptry { // on m c kh nng gy ra ngoi l } catch(ExceptionType e1) { // X l ngoi l 1 } catch(ExceptionType eN) { // X l ngoi l th n } finally{ //Nhm lnh lun c thc hin }
V d finallyimport java.util.*; public class Bai03{ public static void main(String[] args) { Scanner nhap = new Scanner(System.in); try { System.out.println("C gi trn khi nh!"); System.out.println("Nhp vo mt s bt k tm kim: "); int a =nhap.nextInt(); System.out.println("V s tm thy v a c y v!"); } catch (Exception e) { System.out.println("V s: Chng ti khng tm thy!"); }finally{ System.out.println("C y v ri! Khi kim"); } } }
Nm ngoi lT kha throwsKhng nht thit phi x l ngoi l trong phng thc
Khng thng tin x l Khng thm quyn
V dpublic class Test{ public static void main(String[] agrs) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String srt = in.readLine(); // Dng pht sinh ngoi l } }
Danh sch cc ngoi l trong JavaNgoi lException NullPointerException ClassNotFoundException FileNotFoundException SecurityException ArrayIndexOutOfBoundsException IllegalAccessException IllegalArgumentException ArithmeticException NumberFormatException IOException EOFException NoSuchMethodException InterruptedException
Gii thchLp nn ca run-time Mt i tng khng tn ti Khng tm thy Class Khng tm thy file Exception lin quan n bo mt Vt qu ch mc ca mng Truy cp khng hp l i s hm Li thc thi mt php ton nh dng s khng ng Li nhp xut Kt thc mt tp tin Sai tn phng thc Ngt lung ang c thc thi
Checked/ Unchecked ExceptionJava phn bit 2 loi ngoi l RuntimeException Uncheck Exception (Ngoi l khng cn kim tra) Error V cc lp con ca chng
Check Exception (Ngoi l khng cn kim tra)
Gm cc ngoi l cn li
Lan truyn ngoi lPhng thc 3 tung ngoi l Ngoi l lan truynPhng thc 3 Phng thc 2 Phng thc 2 Phng thc 1 Phng thc 1 main() main()
V dpublic class Bai04{ public static void main(String[] args){ methodA(null); } static void methodA(int[] a){ methodB(a); } static void methodB(int[] b){ System.out.println(b[0]); } }
Phng thc ca lp ExceptionCc phng thc di y u k tha t Throwable Exception() Exception(String msg) String getMessage() void printStackTrace() StackTraceElement[] Khi to mt Exception mc nh, null Khi to mt Exception cu thng bo msg Ly cu thng bo ca Exception In ra stack lan truyn ca Exception Mi StackTraceElement l mt i tng cha thngtin v Exception getStackTrace()
Cch d mt ngoi lpublic class Bai05 { public static void main(String[] args) { try { System.out.println("4 chia 5: "+4/0); } catch (Exception e) { System.out.println("Li: "+e); System.out.println(e.getMessage()); System.out.println(e.getClass()); } } }
Ngoi l do ngi dng nh ngha Ngoi vic dng cc lp i tng ngoi l c sn ca Java, ta c th t to ccexception cho ph hp vi chng trnh ca mnh to mt lp ngoi l, k tha t Throwable( Exception) Ci t cc thng bo ph hp. public class MyNumberFormatException extends NumberFormatException { MyNumberFormatException(String s){ System.out.println(s); System.out.println("Khng th convert ch qua s nh"); } }
V dimport java.io.*; public class Bai03{ public static void main(String[] agrs) { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); try { String srt = in.readLine(); char c = srt.charAt(0); boolean b = Character.isLetter(c); if (b==true) throw new MyNumberFormatException("Ngoi l ca tui"); int i = Integer.parseInt(srt); } catch (IOException e) { } catch (MyNumberFormatException e){ } } }
Ch khi s dng ngoi lKhng nn s dng ngoi l thay cho cc lung iu khin trong chng trnh.V d: Kim tra delta trong chng trnh gii phng trnh bc 2.
Nn thit k v s dng ngoi l mt cch thng nht cho ton b d n. Mt s x l li bng ngoi l ph bin l: ht b nh, vt qu ch s mng, con tr null, chia cho 0, i s khng hp l...
Bi tp1.
Vit chng trnh cho php tnh gi tr ca biu thc:5x - y
A=2x + 7y
2.
Yu cu x l cc ngoi l c th xy ra. Vit chng trnh cho php to mt mng 2 chiu c mxn vi m,n nhp t bn phm. Ci t cc x l ngoi l cn thit.
Bi tp3. 4.
Xy dng lp ngoi l DateException cho cc li v ngy thng. Vit chng trnh cho php ngi dng nhp vo ngy, thng nm, nu thng tin ny khng hp l s tung ra mt ngoi l DateException, sau thng bo cho ngi nhp bit v cho php ngi dng nhp li.