Keywords for exceptions.44
Upcoming SlideShare
Loading in...5
×
 

Keywords for exceptions.44

on

  • 428 views

see more details in http://

see more details in http://

Statistics

Views

Total Views
428
Views on SlideShare
428
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Keywords for exceptions.44 Keywords for exceptions.44 Presentation Transcript

  • Usage of Java keywords for Exceptions 1
  • ObjectivesOn completion of this period, you would be able to learn:• Usage of Java keywords to handle exceptions 2
  • RecapIn the previous class, you have studied about how to deal with exception• If an exception occurs within the try block, it is thrown• Your code can catch this exception using catch and handle it in some rational manner 3
  • Usage Of Java Keywords To Handle Exceptions• We know that Java manages exceptions through five keywords: • try • catch • throw • throws • finally 4
  • Keyword Usage in Exception Blocktry { // 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 before try block ends} 5
  • Try and Catch• The default exception handler provided by the Java run-time system is useful for debugging• You will want to handle an exception yourself, for two reasons 1. It allows you to fix the error 2. It prevents the program from automatically terminating 6
  • Simple Java Program Using try- catchclass ExceptionDemo { public static void main(String args[]) { int d, a; try { // to monitor a block of code. d = 0; a = 42 / d; System.out.println("This will not be printed."); } catch (ArithmeticException e) { // catch divide-by-zero error System.out.println("Division by zero."); } System.out.println("After catch statement."); }} 7
  • Explanation• This program is trying to divide a value by zero which is not possible• Hence the program catch Arithmetic Exception and is thrown safely without abnormal termination 8
  • Example 2class ExceptionDemo2 { public static void main (String [] args){ int [] a= new int[3]; try{ for (int i=0; i<=3; i++) a[i]=i*i; System. out. println(a[i]); } } catch(Exception e){ System.out. println(e); } }} 9
  • Explanation Contd . . .• In the above program, an array of size 3 is created• Within the for loop, the array is initialized with i*i• When the value of i in 3, on exception occurs, as the size of array in 3 an its subscript should be less than 3• An exception, ArrayIndexOutOfBoundsException, is handled in the catch block 10
  • Points to Remember• Generally programmer wants handle exceptions for himself for two reasons• One is to fix the error on his own• Another is to prevent the program to terminate automatically• If exception handling mechanism is provided by the programmer, then debugging could be easy 11
  • Summary• In this class we have discussed • Keywords for Java exception handling • The usage of the keywords to handle exceptions 12
  • Quiz1. Which of the following is an example for Arithmetic Exception A. Null pointer B. Stackoverflow C. Divide by zero D. FileNotFound 13
  • Quiz2. ____ keyword is used to monitor for errors A. catch B. try C. finally D. throw 14
  • Frequently Asked Questions1. Write a simple java program which demonstrate the use of try- catch keywords 15
  • Assignment• Modify the program example listed in this lesson, such that no exception are generated 16