In Groovy, you can treat checked exceptions like
unchecked exceptions.
For example, in Java, you need to either catch or throw the checked exception from the method. In Groovy,
you need not do either of these.
For example, below snippet read the contents of a file
line-by-line.
public void readFile(String filePath) throws IOException
{
BufferedReader
br = new BufferedReader(new FileReader(filePath));
String line =
br.readLine();
while (line
!= null) {
System.out.println(line);
line =
br.readLine();
}
br.close();
}
As you see the snippet, I am throwing IOException from
the method readFile. But it is not the case in Groovy, you can avoid of
throwing IOException.
HelloWorld.groovy
public void readFile(String filePath){ BufferedReader br = new BufferedReader(new FileReader(filePath)); String line = br.readLine(); while (line != null) { System.out.println(line); line = br.readLine(); } br.close(); } readFile("C:\\Users\\Krishna\\Downloads\\version.txt")
No comments:
Post a Comment