Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Programming in java by balagurusamy, Lecture notes of Java Programming

lecture notes in java programming

Typology: Lecture notes

2017/2018

Uploaded on 09/24/2018

KarunyaNagarajan
KarunyaNagarajan 🇮🇳

5

(2)

1 document

1 / 67

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit IV
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43

Partial preview of the text

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