can any one explain this piece of code: what is wrong with it?? the compiler says the exception must be caught, but it's handled by the throws isn't it ??
public Test(int inVal) throws Exception{ if (inVal != this.x){ throw new Exception("Invalid input"); } } public static void main(String args[]){ Test t = new Test(4); } }
thanx, prasanthi
Paul Anilprem
Enthuware Software Support
Ranch Hand
Joined: Sep 23, 2000
Posts: 2547
posted
0
>the compiler says the exception must be caught, >but it's handled by the throws isn't it ?? 'throws' does not handle an exception, in fact, it makes the caller handle the exception! >public Test(int inVal) throws Exception This means: I'm going to throw an exception, you better handle it.
>Test t = new Test(4); Now, here you should either catch the exception thrown by Test(4) or pass it on by declaring it in the throws clause of the main method. Handling a Constructor that throws an exception is same as handling a method that throws an exception. HTH, Paul.