Download Programming in java by balagurusamy and more Lecture notes Java Programming in PDF only on Docsity!
Unit IV
Exception
Handling
Illustration of Compile-Time Errors
Class Error { Public static void main(String args[]) { System.out.println(“Hello Java”) //Missing; } }
Error1.java :7: ‘;’ expected
System.out.println (“Hello Jaav”)
^
1 error
Error1.java :7: ‘;’ expected
System.out.println (“Hello Jaav”)
^
1 error
The Most Common Compile Time Errors are
- (^) Missing Semicolons
- (^) Missing (or mismatch of) brackets in classes and methods
- (^) Misspelling of identifiers and keywords
- (^) Missing double quotes in strings
- (^) Use of undeclared variables
- (^) Incompatible types in assignments/initialization
- (^) Bad references to objects
- (^) Use of = in place of == operator
- (^) And so on
The Most Common Compile Time Errors are
- (^) Dividing an integer by zero
- (^) Accessing an element that is not of the bounds of an array
- (^) Trying to store a value into an array of an incompatible class or type
- (^) Trying to cast an instance of a class to one of its subclasses
- (^) Passing a parameter that is not in a valid range or value for a method
- (^) Trying to illegally changes the state of a thread
- (^) Attempting to use a negative size for an array
- (^) Using a null object reference as a legitimate object reference to access a method or a variable
- (^) Converting invalid string to a number
- (^) Accessing a character that is out of bounds of a string
Exception
- (^) An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Exception = Exceptional Event
Error Handling code that performs the
following tasks
- (^) Find the problem ( Hit the exception )
- (^) Inform that an error has occurred ( Throw the exception )
- (^) Receive the error information ( Catch the exception )
- (^) Task corrective actions ( Handle the exception )
Exception Classes
LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException
Exceptions
LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException
Exception describes errors caused
by your program and external
circumstances. These errors can
be caught and handled by your
program
LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException
Runtime Exceptions
Runtime Exception^ Runtime Exception
Syntax of Exception Handling Code
Exception object creator
Exception Handler
Try Block
Statement that causes an exception
Catch Block
Statement that handles the exception
Throws exception object
Example
Class Error { Public static void main(String args[]) { Int a=10; Int b=5; Int c=5; Int x,y; try { x=a/(b-c); } catch (ArithmeticException e) { System.out.println(“Division by Zero”); } Y=a/(b+c); System.out.println(“y=“+y); } }
Output
Division by Zero
Y= 1
Output
Division by Zero
Y= 1
Example
Class Error
Public static void main(String args[])
Int a[]={5,10};
Try
Int x=a[2]/b-a[1];
Catch(ArithmeticException e)
System.out.println(“Division by Zero”);
Catch(ArrayindexOutOfBoundsException e)
System.out.println(“Array Index Error”);
Catch(ArrayStoreException e)
System.out.println(“Wrong data type”);
Int y=a[1]/a[0];
System.out.println(“y=“+y);
Output1:
Array Index Error
Y=
Output1:
Array Index Error
Y=
Using finally Statement
Syntax