Is it necessary that a try block should be followed by a catch block in Java?

Java 8Object Oriented ProgrammingProgramming

Not necessarily catch, a try must be followed by either catch or finally block.

Example

import java.io.File;

public class Test{
   public static void main(String args[]){
      System.out.println("Hello");
      
      try{
         File file = new File("data");
      }
   }
}

Output

C:\Sample>Javac Test.java
Test.java:5: error: 'try' without 'catch', 'finally' or resource declarations
try{
^
1 error
raja
Published on 10-Jan-2018 17:27:13
Advertisements