Thursday, 11 August 2016

Illegal Argument Exception throw catch exception in java

class MyClock
{
int hours ,minutes, seconds;
 MyClock(int hours, int minutes, int seconds)
{
if (hours < 1 || hours > 12)
{
throw new IllegalArgumentException("Hours must be between 1 and 12");
}
if (minutes < 0 || minutes > 59)
{
throw new IllegalArgumentException("Minutes must be between 0 and 59");
}
if (seconds < 0 || seconds > 59)
{
throw new IllegalArgumentException("Seconds must be between 0 and 59");
}
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
System.out.println(hours+"hrs:"+minutes+"mins:"+seconds+"sec;");
}
public MyClock(int hours, int minutes)
{
this(hours, minutes, 0);
}
public MyClock(int hours)
{
this(hours, 0, 0);
}
}
public class ThrowTest
{
public static void main( String args [])
{
try
{
MyClock clock = new MyClock(1, 10,20);
MyClock c=new MyClock(1);
MyClock a=new MyClock(2,50,80);
}
catch( IllegalArgumentException e)
{
System.out.println("IllegalArgumentException is caught...."+e.getMessage());
}
}
}
Post a Comment