کامپیوتر و IT و اینترنتعلوم مهندسی

پردازش استثنایی در جاوا

صفحه 1:
Exception Handling in Java

صفحه 2:
readint موه مه ‎Ce‏ ۴۱02۷] 66 :۱66201010 ع

صفحه 3:
2۵ 000 poe al

صفحه 4:
readint Oe en ۳ 3 00 0

صفحه 5:
#برخورد و مدیریت برخورد ‎(andling the Error)‏ © توقف اجرا © از سر كيرى كارى كه بايد انجام گیرد و یا کار معادل (Propagating the Error) ts 39,5 x." © برنامه ای که اين تابع را فراخوانی کرده مطع کرده و به او اجازه می دهیم که هر طور مایل است با این خطا برخورد کند.

صفحه 6:
#بازكرداندن كد خطا © مقدار بازكشتى تابع با مقدار بازگشتی خطا با هم تداخل پیدا می کنند © برنامه ای که تابع را فراخوانی می کند می بایست بعد از هر بار فراخوانی کد خطا را بررسی کند. *استفاده از یک متغیر سراسری برای اعلام خطا © به طور كلى استفاده از متغير سراسری خوانایی و قابلیت اطمینان برنامه را کاهش می دهد و حتى عيب يابى را نيز مشکل می کند. © برنامه اى كه تابع را فراخوانى مى كند مى بايست بعد از هر بار فراخوانى متغير سراسرى خطا را بررسى كند. ‎al, Exception®‏ حل_هتری_ولیلینک ار لست ‎

صفحه 7:
چگونه می توان یک خطا را توصیف کرد؟ ۵ به کمک یک کد عددی © به کمک یک پیغام متنی © شرايط خطا ممكن است اطلاعاتى بيشتر از یک فیلد داشته باشد می توان یک شرایط خطا را با یک شی بیان کرد.

صفحه 8:
© In Java, the error-handling system is based on exceptions © Exceptions must be handed in a try/catch block © When an exception occurs, process flow is immediately transferred to the catch block © Basic Form try { statement1; statement2; } catch(SomeException someVar) { handleTheException(someVar) ; }

صفحه 9:
Exception #در جاوا دو کلاس برای اشیای خطا در نظر گرفته شده است که همه کلاسهای خطاها از آنها ارث می برند. *خطاهای در بیشتر مواقع از نوع 260610110 هستند. #از آنجایی که شی خطا باید بين توابع مختلف دست به دست گردد باید از یک کلاس که به طور کلی اشیای قابل دست به دست شدن را نمایندگی می کند ‎(Throwable) .»,. +!‏

صفحه 10:
Exception Hierarchy © Simplified Diagram of Exception Hierarchy

صفحه 11:
© Error © A non-recoverable problem that should not be caught (OutOfMemoryError, StackOverflowError, ...) © Exception © An abnormal condition that should be caught and handled by the programmer © RuntimeException © Special case; does not have to be caught © Usually the result of a poorly written program (integer division by zero, array out-of-bounds, etc.) A RuntimeException is considered a bug

صفحه 12:
Multiple Catch Clauses ° A single try can have more 5 one catch clause try { } catch (ExceptionTypel varl) { // Do something } catch (ExceptionType2 var2) { 5 Do something else OE multiple coc clauses are used, order them from the most specific to the most general © Tf no appro) e catch is found, the exception is handed to any ower ry blocks «Ot ‏سس یه‎ Ifno catch clause is fount nae he method, then the exception is thrown ‘by the 9 met

صفحه 13:
Try-Catch, Example BufferedReader in = null; String LineIn; try { in = new BufferedReader(new FileReader("book.txt")); while((lineIn = in.readLine()) != null) { System.out.println(LineIn) ; 1 in.close(); } catch (FileNotFoundException fnfe ) { System.out.printtn("File not found.") } catch (EOFException eofe) { system. out.printin("Unexpected End of File.") } catch (IOException ioe) { System.out.printtn("I0Error reading input: “ + ioe); ioe.printStackTrace(); // Show stack dump

صفحه 14:
The finally Clause ا 0- © After the final catch clause, an optional finally clause may be defined © The finally clause is always executed, even if the try or catch blocks are exited through a break, continue, or return try { } catch (SomeException someVar) { // Do something } finally { // Always executed

صفحه 15:
Thrown Exceptions ——— ee ۶ 1۶ a potential exception is not handled in the method, then the method must declare that the exception can be thrown public SomeType someMethod(...) throws SomeException { // Unhandled potential exception + ° Note: Multiple exception types (comma separated) can be declared in the throws clause ° Explicitly generating an exception throw new IO0Exception("Blocked by firewall."); throw new MalformedURLException("Invalid protocol");

صفحه 16:
® Code that may give rise to an exception must be in a try/catch block or the method must throw the exception © The finally clause is always executed regardless how the try block was exited ced

Exception Handling in Java راه های کلی برخورد با خطاها ‏برخورد و مدیریت برخورد ()Handling the Error ‏ ‏ توقف اجرا از سر گیری کاری که باید انجام گیرد و یا کار معادل ‏پخش کردن خطا ()Propagating the Error ‏ برنامه ای که این تابع را فراخوانی کرده مطع کرده و به او اجازه می دهیم که هر طور مایل است با این خطا برخورد کند. پخش کردن خطا ()Propagating the Error ‏بازگرداندن کد خطا ‏ ‏ مقدار بازگشتی تابع با مقدار بازگشتی خطا با هم تداخل پیدا می کنند برنامه ای که تابع را فراخوانی می کند می بایست بعد از هر بار فراخوانی کد خطا را بررسی کند. ‏استفاده از یک متغیر سراسری برای اعالم خطا ‏ ‏ به طور کلی استفاده از متغیر سراسری خوانایی و قابلیت اطمینان برنامه را کاهش می دهد و حتی عیب یابی را نیز مشکل می کند. برنامه ای که تابع را فراخوانی می کند می بایست بعد از هر بار فراخوانی متغیر سراسری خطا را بررسی کند. @ت @ی@ی@نک@@ار ا@س . Exceptionرا@ه@ ح@لب@@هتریب@@را ا توضیح دلیل خطا و وضعیت حاصل ‏چگونه می توان یک خطا را توصیف کرد؟ ‏ ‏ به کمک یک کد عددی به کمک یک پیغام متنی ‏شرایط خطا ممکن است اطالعاتی بیشتر از یک فیلد داشته باشد ‏می توان یک شرایط خطا را با یک شی بیان کرد. Error Handling: Exceptions In Java, the error-handling system is based on exceptions   Exceptions must be handed in a try/catch block When an exception occurs, process flow is immediately transferred to the catch block Basic Form try { statement1; statement2; ... } catch(SomeException someVar) { handleTheException(someVar); } 8 Basic Java Syntax Exception ‏در جاوا دو کالس برای اشیای خطا در نظر گرفته شده است که همه کالسهای خطاها از آنها ارث می برند. ‏خطاهای در بیشتر مواقع از نوع Exceptionهستند. ‏از آنجایی که شی خطا باید بین توابع مختلف دست به دست گردد باید از یک کالس که به طور کلی اشیای قابل دست به دست شدن را نمایندگی می کند ارث ببرد)Throwable( . Exception Hierarchy Simplified Diagram of Exception Hierarchy Throwable Exception … 10 IOException Error RuntimeException Basic Java Syntax Throwable Types Error  A non-recoverable problem that should not be caught (OutOfMemoryError, StackOverflowError, …) Exception  An abnormal condition that should be caught and handled by the programmer RuntimeException   Special case; does not have to be caught Usually the result of a poorly written program (integer division by zero, array out-of-bounds, etc.)  11 A RuntimeException is considered a bug Basic Java Syntax Multiple Catch Clauses A single try can have more that one catch clause   try { ... } catch (ExceptionType1 var1) { // Do something } catch (ExceptionType2 var2) { // Do something else } If multiple catch clauses are used, order them from the most specific to the most general If no appropriate catch is found, the exception is handed to any outer try blocks  12 If no catch clause is found within the method, then the exception is thrown by the method Basic Java Syntax Try-Catch, Example ... BufferedReader in = null; String lineIn; try { in = new BufferedReader(new FileReader("book.txt")); while((lineIn = in.readLine()) != null) { System.out.println(lineIn); } in.close(); } catch (FileNotFoundException fnfe ) { System.out.println("File not found."); } catch (EOFException eofe) { System.out.println("Unexpected End of File."); } catch (IOException ioe) { System.out.println("IOError reading input: " + ioe); ioe.printStackTrace(); // Show stack dump } 13 Basic Java Syntax The finally Clause After the final catch clause, an optional finally clause may be defined The finally clause is always executed, even if the try or catch blocks are exited through a break, continue, or return try { ... } catch (SomeException someVar) { // Do something } finally { // Always executed } 14 Basic Java Syntax Thrown Exceptions If a potential exception is not handled in the method, then the method must declare that the exception can be thrown public SomeType someMethod(...) throws SomeException { // Unhandled potential exception ... }  Note: Multiple exception types (comma separated) can be declared in the throws clause Explicitly generating an exception throw new IOException("Blocked by firewall."); throw new MalformedURLException("Invalid protocol"); 15 Basic Java Syntax Summary Code that may give rise to an exception must be in a try/catch block or the method must throw the exception  16 The finally clause is always executed regardless how the try block was exited Basic Java Syntax

51,000 تومان