



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




COLLEGE OF COMPUTER AND INFORMATION SCIENCES INFORMATION TECHNOLOGY DEPARTMENT
CSC 11 3 : Java Programming 2 Tutorial 7 2 nd^ Semester 14 41 - 1442
Q1: Mention if the following code fragment are legal or illegal. Explain your choice, and specify the output (if any).
a) public static void main(String[] args){ try{ int[] nums = new int[10]; sub(nums); } catch(Exception e) { System.out.println(e);} } public static void sub(int[] nums){ nums[15] = 15;}
b) static Scanner input = new Scanner(System.in); public static void main(String[] args) { inti; try{ i= input.nextInt(); } catch (Exception e) { System.out.println("first exception handled"); } catch (InputMismatchException e) { System.out.println("Second exception handled"); } }
c) public static void main (String args[]) { problem(); } public static void problem () { try { System.out.println("A"); return; }
COLLEGE OF COMPUTER AND INFORMATION SCIENCES INFORMATION TECHNOLOGY DEPARTMENT CSC 11 3 : Java Programming 2 Tutorial 7 2 nd^ Semester 14 41 - 1442
catch(Exception e){ System.out.println("B"); } finally { System.out.println("C"); } } d) public static void A() throws MyException { ... } public static void B() { A();}
e) public static void A() throws MyException { ... } public static void B()throws MyException { A();}
f) public static void A() throws MyException { ... } public static void B() { try{ A(); } catch (MyExeception e){ System.out.println(e); }} }}
public class NegativeNumberException extends Exception { public NegativeNumberException (String m){ super(m);} }
public class ZeroException extends RuntimeException { public ZeroException(String m) { super(m);} }
COLLEGE OF COMPUTER AND INFORMATION SCIENCES INFORMATION TECHNOLOGY DEPARTMENT CSC 11 3 : Java Programming 2 Tutorial 7 2 nd^ Semester 14 41 - 1442
ExceptionExercise ex = new ExceptionExercise(); try{ // add try body here } catch(ZeroException e) { System.out.println(e.getMessage());}
catch(NegativeNumberException e) { System.out.println(e.getMessage()); System.out.println("Caught in main"); }
catch(ArrayIndexOutofBoundsException e) { System.out.println(e);
} System.out.println("End of main"); } }
COLLEGE OF COMPUTER AND INFORMATION SCIENCES INFORMATION TECHNOLOGY DEPARTMENT CSC 11 3 : Java Programming 2 Tutorial 7 2 nd^ Semester 14 41 - 1442
public class MoreThan0Char extends Exception {...} public class MoreThan5Char extends MoreThan0Char {...} public class MoreThan10Char extends MoreThan5Char {...} public class Test { public static void main (String [] args) { Scanner input = new Scanner (System.in); String x = input.next(); try{ try{ if(x.length()>10) throw new MoreThan10Char(); else if (x.length() >5) throw new MoreThan5Char(); else if (x.length()>1) throw new MoreThan0Char(); } catch (MoreThan5Char e) {System.out.print ("MoreThan5Char caught");} } catch (MoreThan10Char e) {System.out.print ("MoreThan10Char caught");} catch (MoreThan0Char e) {System.out.print ("MoreThan0Char caught");} }} input output Up Exercise Congratulations