Hi, I am bit confused. Tell me if a block of code doesnot throw any exception, its valid to enter in try and catch block right. But sometime back, when I wrote some code, it gave me compiler error, saying that it is not throwing any exception, so there is no need of try or catch block. It will be good if somebody explains me with code. Thanks in advance
Arathi : I believe, the code you write inside your try{} block must throw the kind of exception that you are catching in your catch block. If you write
then the compiler will complain. This is the message I get Foo.java:9: Exception java.io.IOException is never thrown in the body of the corresponding try statement. catch (IOException e){ // some exception catching code here ^ HTH [ January 16, 2002: Message edited by: Shivaji Marathe ] [ January 16, 2002: Message edited by: Shivaji Marathe ]
Originally posted by Arathi Rajashekar: Hi, I am bit confused. Tell me if a block of code doesnot throw any exception, its valid to enter in try and catch block right. But sometime back, when I wrote some code, it gave me compiler error, saying that it is not throwing any exception, so there is no need of try or catch block. It will be good if somebody explains me with code. Thanks in advance
If you catch an non-checked exception , without throwing it or calling/doing anything that can throw it in your try block then you will get a compile time error
However if the exception is non-checked like nullptr or class cast then it doesnt matter wheather you catch or not , it will still compile becoz the programmer is not obligated to do it at compile time Ragu