A)
class throwsexample
{
void x() throws InterruptedException
{
Thread.sleep(1000);
}
void y()
{
x();
}
void z()
{
y();
}
}
->the above code cause compilation error at x() class in y() method.
->Reason is x method has “checkedException” in it specification.
->we can fix the in two ways.
Case 1:
void y() throws InterruptedException
{
x();
}
Case 2:
void y()
void y()
{
try
{
x();
}
catch(Exception e) //or catch(InterruptedException e)
{ }
}
Note :
If we use case 1 solution, the above code causes another compilation error. Because z() is calling y() method.
Comments
Post a Comment