Exceptions
- What is the output of this program?
public class Exec_Handling_Example
{
public static void main(String args[])
{
try
{
int k, n;
n = 5;
for (k = -2; k < 2 ;++k)
{
n = (n / k);
System.out.print(k);
}
}
catch(ArithmeticException e)
{
System.out.print("0");
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed.
- Which of these keywords is used to generate an exception explicitly?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
throw
- Which of these class is related to all the exceptions that are explicitly thrown?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
Throwable
- What is the output of this program?
public class exceptionHandling_Example
{
public static void main(String args[])
{
try
{
int n = args.length;
int n1 = 9 / n;
System.out.print(n);
try
{
if (n == 1)
n = n / n - n;
if (n == 2)
{
int s = {1};
s[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("Hello");
}
catch (ArithmeticException e)
{
System.out.println("Java");
}
}
}
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Compilation Error
error: 'try' without 'catch', 'finally' or resource declarations
try
^
1 error
- Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.