package net.dharmaraj;
class Level1Exception extends Exception
{
}
class Level2Exception extends Level1Exception
{
}
class Level3Exception extends Level2Exception
{
}
class Purple
{
public static void main(String args[])
{
int a, b, c, d, f, g, x;
a = b = c = d = f = g = 0;
x = 2;
try
{
try
{
switch (x) {
case 1:
throw new Level1Exception();
case 2:
throw new Level2Exception();
case 3:
throw new Level3Exception();
}
a++;
}
catch (Level2Exception e)
{
b++;
}
finally
{
c++;
}
}
catch (Level1Exception e)
{
d++;
}
catch (Exception e)
{
f++;
}
finally
{
g++;
}
System.out.println(a + "," + b + "," + c + "," + d + "," + f + "," + g);
}
}
Exception
Saturday, 25 May 2013
Exception - 25 -
package net.dharmaraj;
public class ThrowsDemo
{
static void throwMethod() throws IllegalAccessException
{
System.out.println("Inside throwMetho.");
throw new IllegalAccessException("demo");
}
public static void main(String a[])
{
// ThrowsDemo s=new ThrowsDemo();
try
{
throwMethod();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
Out Put:--
Inside throwMetho.
Caught java.lang.IllegalAccessException: demo
Exception - 24 -
package net.dharmaraj;
public class TechnoSample
{
public static void main(String args[])
{
int i = 2;
try
{
double x = Math.random() * 2;
System.out.println(x);// 0.4545
i *= (int) Math.floor(x);
System.out.println(i);// 0
if (i > 1)
System.out.println("No arithmetic exception");
}
catch (ArithmeticException ae)
{
System.out.println("Arithmetic exception cautht");
}
finally
{
System.out.println(i);
}
}
}
Out Put:--
1.5059545776537702
2
No arithmetic exception
2
public class TechnoSample
{
public static void main(String args[])
{
int i = 2;
try
{
double x = Math.random() * 2;
System.out.println(x);// 0.4545
i *= (int) Math.floor(x);
System.out.println(i);// 0
if (i > 1)
System.out.println("No arithmetic exception");
}
catch (ArithmeticException ae)
{
System.out.println("Arithmetic exception cautht");
}
finally
{
System.out.println(i);
}
}
}
Out Put:--
1.5059545776537702
2
No arithmetic exception
2
Exception - 23 -
package net.dharmaraj;
public class TechnoSample
{
public static void main(String[] args) throws Exception
{
int i = 2;
boolean b = true;
throw new Exception("Values are:" + (b != b) + "," + (i = args.length) + "," + (b = i == 2));
}
}
Out Put :
Exception in thread "main" java.lang.Exception: Values are:false,0,false
at net.dharmaraj.TechnoSample.main(TechnoSample.java:9)
public class TechnoSample
{
public static void main(String[] args) throws Exception
{
int i = 2;
boolean b = true;
throw new Exception("Values are:" + (b != b) + "," + (i = args.length) + "," + (b = i == 2));
}
}
Out Put :
Exception in thread "main" java.lang.Exception: Values are:false,0,false
at net.dharmaraj.TechnoSample.main(TechnoSample.java:9)
Exception - 22 -
Imagine there are two exception classes called Exception1 and Exception2 that descend from the Exception class. Given these two class definitions:
package net.dharmaraj;
class First
{
void test() throws Exception1, Exception2 { . . . }
}
class Second extends First
{
void test() { . . . }
}
Create a class called Third that extends Second and defines a test()
method. What exceptions can Third's test() method throw?
Select all valid answers.
a) Exception1
b) Exception2
c) no checked exceptions
d) any exceptions declared in the throws clause of the Third's test() method.
package net.dharmaraj;
class First
{
void test() throws Exception1, Exception2 { . . . }
}
class Second extends First
{
void test() { . . . }
}
Create a class called Third that extends Second and defines a test()
method. What exceptions can Third's test() method throw?
Select all valid answers.
a) Exception1
b) Exception2
c) no checked exceptions
d) any exceptions declared in the throws clause of the Third's test() method.
Exception - 21 -
Given this code snippet:
try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}
What will appear in the standard output if tryThis() throws a NumberFormatException?
Select the one right answer.
a) Nothing
b) "exception 1", followed by "finally"
c) "exception 2", followed by "finally"
d) "exception 1"
e) "exception 2"
try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}
Answers.:- c
try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}
What will appear in the standard output if tryThis() throws a NumberFormatException?
Select the one right answer.
a) Nothing
b) "exception 1", followed by "finally"
c) "exception 2", followed by "finally"
d) "exception 1"
e) "exception 2"
try {
tryThis();
return;
} catch (IOException x1) {
System.out.println("exception 1");
return;
} catch (Exception x2) {
System.out.println("exception 2");
return;
} finally {
System.out.println("finally");
}
Answers.:- c
Exception - 20 -
What all gets printed when the following gets compiled and run. Select the two correct answers.
package net.dharmaraj;
public class test
{
public static void main(String args[])
{
int i = 1, j = 1;
try
{
i++;
j--;
if (i == j)
i++;
}
catch (ArithmeticException e)
{
System.out.println(0);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(1);
}
catch (Exception e)
{
System.out.println(2);
}
finally
{
System.out.println(3);
}
System.out.println(4);
}
}
a) 0
b) 1
c) 2
d) 3
e) 4
Out Put:--
3
4
package net.dharmaraj;
public class test
{
public static void main(String args[])
{
int i = 1, j = 1;
try
{
i++;
j--;
if (i == j)
i++;
}
catch (ArithmeticException e)
{
System.out.println(0);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(1);
}
catch (Exception e)
{
System.out.println(2);
}
finally
{
System.out.println(3);
}
System.out.println(4);
}
}
a) 0
b) 1
c) 2
d) 3
e) 4
Out Put:--
3
4
Subscribe to:
Posts (Atom)