Intro to java ( exceptions chapter) 1. Correct the following method definition b
ID: 3914115 • Letter: I
Question
Intro to java ( exceptions chapter)
1. Correct the following method definition by adding a suitable throws clause: (5 POINTS)
public void doStuff(int n)
{
if (n < 0)
throw new Exception("Negative number.");
}
2. What happens if you invoke a method and it throws a checked exception that it does not catch? (5 POINTS)
3. What output will be produced by the following code? The definition of the class NegativeNumberException is given in the preceding material, but you do not need to look at it to answer this question.
try
{
int n = 7;
if (n > 0)
throw new Exception();
else if (n < 0)
throw new NegativeNumberException();
else
System.out.println("Hello!");
}
catch(NegativeNumberException e)
{
System.out.println("First catch.");
}
catch(Exception e)
{
System.out.println("Second catch");
}
System.out.println("End of code");
4. Repeat Question 3, but change the value of n from 7 to ?7. (5 POINTS)
5. Repeat Question 3, but change the value of n from 7 to 0. (5 POINTS)
6. What output is produced by the following program? (5 POINTS)
public class Question30
{
public static void main(String[] args)
{
Question30 object = new Question30();
try
{
System.out.println("Trying");
object.sampleMethod();
System.out.println("Trying after call.");
}
catch(Exception e)
{
System.out.println("Catching");
System.out.println(e.getMessage());
}
}
public void sampleMethod() throws Exception
{
System.out.println("Starting sampleMethod.");
throw new Exception("From sampleMethod with love.");
}
}
7. Consider the following program:
public class Question33
{
public static void main(String[ ] args)
{
try
{
sampleMethod(99);
}
catch(Exception e)
{
System.out.println("Caught in main.");
}
}
public static void sampleMethod(int n) throws Exception
{
try
{
if (n > 0)throw new Exception();
else if (n < 0)
throw new NegativeNumberException();
else
System.out.println("No exception.");
System.out.println("Still in sampleMethod.");
}
catch (NegativeNumberException e)
{
System.out.println("Caught in sampleMethod.");
}
finally
{
System.out.println("In finally block.");
}
System.out.println("After finally block.");
}
}
a. What output does the program produce? (5 POINTS)
b. What output would the program produce if the argument to sampleMethod were ?99 instead of 99? (5 POINTS)
c. What output would the program produce if the argument to sampleMethod were 0 instead of 99? (5 POINTS)
Explanation / Answer
1) Exception causing code must be in try catch block. Corect way of doing this is below
public void doStuff(int n)
{
if (n < 0)
try {
throw new Exception("Negative number.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2)It will give compile time error. All checked exception must be caught or thrown at compile time only
3)It will print Second Catch follwed by End of Code
since n>0 it will throw Eception() which will be caught by second catch block
4)for n=-7 it willthrow NeageativeNumberEception and first catch will be executed and it will print
First Catch
End of Code
5) if n==0 then else is executed and
Hello!
End of Code
is the output
6)Output will be:
Trying
Starting sampleMethod.
Catching
From sampleMethod with love.
Explanation:
First it will enter Try block and it will print Trying then sampleMethod() will be calles in that it will print Starting sampleMethod then it will throw exception which was not catched by sampleMethod()
so now it will be propagated back to main() and it has the code to catch that exception so in catch it will print Carching follwed by From sampleMethod with love
7)
a)In finally block.
Caught in main.
will be the output
in sample method there is no catch to handle the Exception so finally will be executed and then in main() catch will be executed.
b)Caught in sampleMethod.
In finally block.
After finally block.
since sampleMethod() throws the exception catch and finally in that method will be executed
C)No exception.
Still in sampleMethod.
In finally block.
After finally block.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.