صفحه 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
