Exception Handling: try-catch-finally example
package com.ram; import java.io.BufferedReader; import java.io.InputStreamReader; public class ExceptionHandlingExample2 { public static void main(String[] args) { String name = ""; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Entere a name:: "); name = br.readLine(); char firstName[] = name.toCharArray(); for(int i=0;i<=20;i++){ System.out.print(firstName[i]); } }catch(ArrayIndexOutOfBoundsException aiobe){ System.out.println(" An ArrayIndexOutOfBoundsException occured = "+aiobe); } catch(Exception e){ System.out.println("Any other exception "+e); }finally{ System.out.println("Name = "+name); } } }
Execute ExceptionHandlingExample2 class and user would be prompted to enter a value.
Entere a name:: Sachin Tendulkar
Sachin Tendulkar An ArrayIndexOutOfBoundsException occured = java.lang.ArrayIndexOutOfBoundsException: 16
Name = Sachin Tendulkar
Entere a name:: Sachin Tendulkar
Sachin Tendulkar An ArrayIndexOutOfBoundsException occured = java.lang.ArrayIndexOutOfBoundsException: 16
Name = Sachin Tendulkar
No comments:
Post a Comment