Question 1
class Test extends Exception { }
class Main {
public static void main(String args[]) {
try {
throw new Test();
}
catch(Test t) {
System.out.println(\"Got the Test Exception\");
}
finally {
System.out.println(\"Inside finally block \");
}
}
}
Question 2
class Main {
public static void main(String args[]) {
int x = 0;
int y = 10;
int z = y/x;
}
}
Question 3
class Test
{
public static void main(String[] args)
{
try
{
int a[]= {1, 2, 3, 4};
for (int i = 1; i <= 4; i++)
{
System.out.println (\"a[\" + i + \"]=\" + a[i] + \"\\n\");
}
}
catch (Exception e)
{
System.out.println (\"error = \" + e);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println (\"ArrayIndexOutOfBoundsException\");
}
}
}
Question 4
class Test
{
public static void main (String[] args)
{
try
{
int a = 0;
System.out.println (\"a = \" + a);
int b = 20 / a;
System.out.println (\"b = \" + b);
}
catch(ArithmeticException e)
{
System.out.println (\"Divide by zero error\");
}
finally
{
System.out.println (\"inside the finally block\");
}
}
}
Question 5
class Test
{
String str = \"a\";
void A()
{
try
{
str +=\"b\";
B();
}
catch (Exception e)
{
str += \"c\";
}
}
void B() throws Exception
{
try
{
str += \"d\";
C();
}
catch(Exception e)
{
throw new Exception();
}
finally
{
str += \"e\";
}
str += \"f\";
}
void C() throws Exception
{
throw new Exception();
}
void display()
{
System.out.println(str);
}
public static void main(String[] args)
{
Test object = new Test();
object.A();
object.display();
}
}
Question 7
Question 8
class Test
{ int count = 0;
void A() throws Exception
{
try
{
count++;
try
{
count++;
try
{
count++;
throw new Exception();
}
catch(Exception ex)
{
count++;
throw new Exception();
}
}
catch(Exception ex)
{
count++;
}
}
catch(Exception ex)
{
count++;
}
}
void display()
{
System.out.println(count);
}
public static void main(String[] args) throws Exception
{
Test obj = new Test();
obj.A();
obj.display();
}
}
Question 9
class Base extends Exception {}
class Derived extends Base {}
public class Main {
public static void main(String args[]) {
// some other stuff
try {
// Some monitored code
throw new Derived();
}
catch(Base b) {
System.out.println(\"Caught base class exception\");
}
catch(Derived d) {
System.out.println(\"Caught derived class exception\");
}
}
}
Question 10
class Main {
public static void main(String args[]) {
try {
throw 10;
}
catch(int e) {
System.out.println(\"Got the Exception \" + e);
}
}
}
There are 10 questions to complete.