Why doesn't my code compile?
Here's the code:
public class Exceptions { public void method1(int x) { if (x < 0) throw new Exception("ERROR: Negative value in X coordinate."); } public static void main(String[] args) { Exceptions ex = new Exceptions(); try { ex.method1(-1); } catch (Exception e) { System.out.print(e.getMessage()); e.printStackTrace(); } } }
Error message:
Exceptions.java:6: error: unreported exception Exception; must be caught or declared to be thrown
throw new Exception("ERROR: Negative value in X coordinate.");
^
1 error