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?