Chapter v(error)
-
Upload
chhom-karath -
Category
Education
-
view
135 -
download
0
Transcript of Chapter v(error)
![Page 1: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/1.jpg)
Errors - Exceptions
![Page 2: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/2.jpg)
• Object describes an exceptional (that is, error) condition that has occurred in a piece of code.
• An exception is an abnormal condition that arises in a code sequence at run time.try {
// block of code to monitor for errors}catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1}catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2}// ...finally {
// block of code to be executed after try block ends}
![Page 3: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/3.jpg)
Error=Run || Comple
• try{….}catch(typeofException){….}• try{….}catch(….){….}…catch(..){….}• try{….}catch(…){…}…catch(…){…}..finally{…}– try=check error– catch=detect error– throw=making error occur– throws=throw error.– Finally=end of choice
![Page 4: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/4.jpg)
Exception Types
• RuntimeException: subclass of Exception– automatically defined for the programs that you
write and include things such as division by zero and invalid array indexing.
• Error– not expected to be caught under normal
circumstances by your program ;Stack overflow.
![Page 5: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/5.jpg)
RuntimeException: execute
Erro: compile
![Page 6: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/6.jpg)
class Except1{ public static void main(String args[]) { int d, a; try{ d=0; a=13/d; }catch(ArithmeticException e) { System.out.println("Cannot divided by 0 te." );
}}}
Using try and catch
![Page 7: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/7.jpg)
Multiple catch Clausesclass MultiCatch {public static void main(String args[]) {try {int a = args.length;System.out.println("a = " + a);int b = 42 / a;int c[] = { 1 };c[42] = 99;} catch(ArithmeticException e) {System.out.println("Divide by 0: " + e);} catch(ArrayIndexOutOfBoundsException e) {System.out.println("Array index oob: " + e);}System.out.println("After try/catch blocks.");}}
![Page 8: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/8.jpg)
throw• Catching exceptions that are thrown by the Java run-time system.• throw ThrowableInstance;class ThrowDemo {static void demoproc() {
try {throw new NullPointerException("demo"); } catch(NullPointerException e) {System.out.println("Caught inside demoproc.");throw e; // rethrow the exception }
}public static void main(String args[]) {try {
demoproc();} catch(NullPointerException e) {System.out.println("Recaught: " + e);
}}
}=>Caught inside demoproc.Recaught: java.lang.NullPointerException: demo
![Page 9: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/9.jpg)
import java.util.Random;class HandleError {public static void main(String args[]) {int a=0, b=0, c=0;Random r = new Random();for(int i=0; i<32000; i++) {try {b = r.nextInt();c = r.nextInt();a = 12345 / (b/c);} catch (ArithmeticException e) {System.out.println("Division by zero.");a = 0; // set a to zero and continue}System.out.println("a: " + a);}}}
![Page 10: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/10.jpg)
throws
class ThrowsDemo{
static void throwOne() throws IllegalAccessException {
int a,b;a=10;b=0;if(b==0)
{throw new IllegalAccessException("Cannot divide by zerto
te");}
elseSystem.out.println(a + "/" + b + "=" + a/b);
}public static void main(String args[]) {
try {throwOne();
} catch (IllegalAccessException e)
{System.out.println("Caught: " + e);
}}}
![Page 11: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/11.jpg)
finally// Demonstrate finally.class FinallyDemo {// Through an exception out of the method.
static void procA() {try {
System.out.println("inside procA");throw new RuntimeException("demo");}
finally {System.out.println("procA's finally");}
}// Return from within a try block.static void procB() {
try {System.out.println("inside procB");return;}
finally {System.out.println("procB's finally");
}}// Execute a try block normally.static void procC() {
try {System.out.println("inside procC");
}finally {
System.out.println("procC's finally");}
}public static void main(String args[]) {
try {procA();
} catch (Exception e) {
System.out.println("Exception caught");}procB();procC();
}}
![Page 12: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/12.jpg)
Java’s Built-in Exceptions
![Page 13: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/13.jpg)
![Page 14: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/14.jpg)
Rethrowing Exceptions• if the handler cannot process the exception or simply wants to let
its caller be notified of the exception.• try {
statements;}catch (TheException ex) {perform operations before exits;throw ex;}
![Page 15: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/15.jpg)
Chained Exceptions
![Page 16: Chapter v(error)](https://reader036.fdocuments.net/reader036/viewer/2022083104/587f7e641a28ab3f4e8b6621/html5/thumbnails/16.jpg)
Creating Your Own Exception Subclasses