0% found this document useful (0 votes)
12 views

Week 7 B

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Week 7 B

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

b) In the CustomExceptionTestclass, the age is expected to be a positive number.

It would
throw the user defined exception NegativeAgeException if the age is assigned a negative
number.

class NegativeAgeException extends Exception

NegativeAgeException(String s)

super(s);

public String toString()

return "Age Exception";

public class ExcepDemo2

static void validate(int age) throws NegativeAgeException

if(age<0)

throw new NegativeAgeException(" not valid "+age);

else

System.out.println(" welcome to the world " +age);

public static void main(String args[])


{

try{

validate(23);

validate(17);

validate(-12);

validate(25); // Not Executed

catch(Exception m)

System.out.println("Exception occured: No Negative Age");

System.out.println("--- " + m + "---"); // Description of Message

finally {

System.out.println("Finally block executed");

System.out.println("rest of the code...");

You might also like