Docsity

Java Programming 2 Tutorial 7: Exception Handling in Java, Assignments of Java Programming

Partial preview of the text

Download Java Programming 2 Tutorial 7: Exception Handling in Java and more Assignments Java Programming in PDF only on Docsity!

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); }} }}

Q2: Given the following Java program

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"); } }

Determine the output when the body of the try is:

a. ex.method2(0);

b. ex.method2(1);

c. ex.method2(2);

d. ex.method2(3);

e. ex.method2(4);

COLLEGE OF COMPUTER AND INFORMATION SCIENCES INFORMATION TECHNOLOGY DEPARTMENT CSC 11 3 : Java Programming 2 Tutorial 7 2 nd^ Semester 14 41 - 1442

Q3: Determine the output of the following program when the input:

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