Question

Java NetBeans project ***please show solution**** public class JavaApplication1 {     public static void main(String[] args)...

Java NetBeans project ***please show solution****

public class JavaApplication1 {

    public static void main(String[] args) {

        RuntimeException exception = null;

        throw exception;

    }

}

Test Stem / Question

Choices

What is the problem with the above code?

A: null exception cannot be thrown

B: the main method must be declared with throws statement

C: It is illegal to throw an exception

D: Nothing is wrong

How should you fix the code? Choose from the following:

  1. Instantiate the exception
  2. Add a throws statement in the method declaration
  3. Do no throw the exception

A: A & B

B: A only

C: B only

D: All is correct

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) When you run first code following is the out put :

package HomeworkLib;

public class JavaApplication1 {
   public static void main(String[] args) {
       RuntimeException exception = null;
       throw exception;
   }
}

Answer : A) null exception can not be thrown

2)

package HomeworkLib;

public class JavaApplication1 {
   public static void main(String[] args) throws Exception {
       RuntimeException exception = new RuntimeException("new exception");
       throw exception;
   }
}


Answer :

1) A &B

Add a comment
Know the answer?
Add Answer to:
Java NetBeans project ***please show solution**** public class JavaApplication1 {     public static void main(String[] args)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT