Java Programming Exercise: Handling Custom Exceptions, Assignments of Java Programming

Java programming tutorial materials for csc 113: java programming 2, specifically tutorial 8, exception 2, fall 2020. It includes examples of custom exception classes (underzeroexception and zeroexception) and their usage in method1 and method2. The document also presents a question asking to determine the output when calling different methods with various arguments.

Typology: Assignments

2020/2021

Uploaded on 04/20/2021

shamma-alshamikh
shamma-alshamikh 🇺🇸

5

(1)

6 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
King Saud University
College of Computer and Information Sciences
Computer Science Department
CSC#113:#Java#Programming#2#
Tutorial#8#
Exception#2#
Fall#2020#
Exceptions+-+2+
1+
Q1: Given the following Java program:
public class UnderZeroException extends Exception {
public UnderZeroException (String m){
super(m);} }
public class ZeroException extends RuntimeException {
public ZeroException(String m) {
super(m);} }
public class ExceptionExercise{
private int[] numbers = {1,-3,-5,0};
private int x;
public void method1(int y) throws UnderZeroException {
System.out.println("Start of method1");
int temp = numbers[y];
x= y+1;
if (temp <0)
throw new UnderZeroException("method1:The number is
negative!");
else if(temp == 0)
throw new ZeroException("method1:The number is zero");
System.out.println("End of method1");
}
public void method2(int n) throws UnderZeroException {
System.out.println("Start of method2");
try{
method1(n);}
catch(UnderZeroException e)
{
System.out.println(e);
if(x>2)
throw e;
else
throw new UnderZeroException("method2:The number is
negative!"); }
finally
{System.out.println("Finally in method2");}
pf3
pf4

Partial preview of the text

Download Java Programming Exercise: Handling Custom Exceptions and more Assignments Java Programming in PDF only on Docsity!

College of Computer and Information Sciences

Computer Science Department

CSC 113: Java Programming 2

Tutorial 8

Exception 2

Fall 2020

Q1: Given the following Java program:

public class UnderZeroException extends Exception { public UnderZeroException (String m){ super(m);} } public class ZeroException extends RuntimeException { public ZeroException(String m) { super(m);} } public class ExceptionExercise{ private int[] numbers = {1,-3,-5,0}; private int x; public void method1(int y) throws UnderZeroException { System.out.println("Start of method1"); int temp = numbers[y]; x= y+1; if (temp <0) throw new UnderZeroException("method1:The number is negative!"); else if(temp == 0) throw new ZeroException("method1:The number is zero"); System.out.println("End of method1"); } public void method2(int n) throws UnderZeroException { System.out.println("Start of method2"); try{ method1(n);} catch(UnderZeroException e) { System.out.println(e); if(x>2) throw e; else throw new UnderZeroException("method2:The number is negative!"); } finally {System.out.println("Finally in method2");}

College of Computer and Information Sciences

Computer Science Department

CSC 113: Java Programming 2

Tutorial 8

Exception 2

Fall 2020

System.out.println("End of method2"); } //------------------- public static void main(String[] args){ System.out.println("Start Of main"); ExceptionExercise ex = new ExceptionExercise(); try{ // ... The body of try } catch(ZeroException e) { System.out.println(e.getMessage());} catch(UnderZeroException 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 try is:

a) ex.method2(0)

b) ex.method2(1)

c) ex.method2(2)

d) ex.method2(3)

e) ex.method1(4)

College of Computer and Information Sciences

Computer Science Department

CSC 113: Java Programming 2

Tutorial 8

Exception 2

Fall 2020

Q3: Determine the output of the following program when the input is 1, 8, and 12:

public class GreaterThan0 extends Exception {…} public class GreaterThan6 extends GreaterThan0 {…} public class GreaterThan11 extends GreaterThan6 {…} public static void main (String [] args) { Scanner input = new Scanner (System.in); int x = input.nextInt(); try{ try{ if(x>11) throw new GreaterThan11(); else if (x >6) throw new GreaterThan6(); else if (x>0) throw new GreaterThan0(); } catch (GreaterThan6 e) {System.out.print ("GreaterThan6 caught");} } catch (GreaterThan11 e) {System.out.print ("GreaterThan11 caught");} catch (GreaterThan0 e) {System.out.print ("GreaterThan0 caught");} } }