1

Here is a super simple java code

public class Main {


    public static void throwExample() {
        throw new Exception(); // IntelliJ says that something's wrong here.
    }

    public static void main(String[] args) throws FileNotFoundException {

        FileOutputStream fos = new FileOutputStream("aa.txt");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        for(int i = 0; i < 10 ; i++) {
            baos.write((byte) i);
        }

        System.out.println(Arrays.toString(baos.toByteArray()));
    }
}

On the third line I commented, IntelliJ says that there's something wrong here. So I randomly changed the code and, when I did the things like below,

public static void throwExample() // throws Exception

it became ok.

But I saw lots of examples using 'throw' keyword without 'throws' together, such as

class Exception2{
   static int sum(int num1, int num2){
      if (num1 == 0)
         throw new ArithmeticException("First parameter is not valid");
      else
         System.out.println("Both parameters are correct!!");
      return num1+num2;
   }
   public static void main(String args[]){
      int res=sum(0,12);
      System.out.println(res);
      System.out.println("Continue Next statements");
   }
}

What would the problem in my code?

Now.Zero
  • 1,147
  • 1
  • 12
  • 23
  • 1
    `Exception` is checked. Add `throws Exception` to method – ernest_k Feb 24 '19 at 10:47
  • 3
    Well `throw new Exception;` isn't a valid statement for a start, because you haven't provided an argument list to the constructor. You meant `throw new Exception();`. Then there's the checked aspect to it, as ernest_k mentioned. Note that your question would be better if you'd provide the *exact* error message rather than just "IntelliJ says that there's something wrong here". Error messages are important. – Jon Skeet Feb 24 '19 at 10:47
  • 2
    Read some theoretical aspects of Java about exceptions. There are many and you need to know which of them you need and which of them you don't. – dpapadopoulos Feb 24 '19 at 10:48
  • @JonSkeet I did as you said, but still errors appear – Now.Zero Feb 24 '19 at 10:48
  • I suggest you read the error messages very carefully then, and try to understand what's wrong. We can't help you with code we can't see providing error message you haven't stated. – Jon Skeet Feb 24 '19 at 10:51
  • Suggested reading for improving your questions: https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – Jon Skeet Feb 24 '19 at 10:52

2 Answers2

3

But I saw lots of examples using 'throw' keyword without 'throws' together

In java, there are two main categories of exceptions which are

  1. Checked exceptions
  2. Unchecked exceptions

If you throw a checked exception, you need to catch it by either surrounding the piece of code that could throw the checked exception with try catch block or by adding throws keyword in method signature.

On the other hand, if your code throws unchecked exception, it is not necessary to catch it. Hence java compiler doesn't complains about you not adding a throws keyword in method signature in your second code example.

Yousaf
  • 27,861
  • 6
  • 44
  • 69
1

Exception is a CheckedException it needs a throws. All exceptions are inherit from RuntimeException are UncheckedException and dont need a try/catch or throws.

More: Java: checked vs unchecked exception explanation

pL4Gu33
  • 2,045
  • 16
  • 38
  • Thank you! Then, in the opposite way, would CheckedException would need either try/catch or throws? – Now.Zero Feb 24 '19 at 10:51