Java error illegal start of type

The Java error illegal start of type occurred in your code when the starting braces of catch block is not used after the close of try block.

Java error illegal start of type

Java error illegal start of type

     

The Java error illegal start of type occurred in your code when the starting braces of catch block  is not used after the close of try block. 

Understand with Example

In this tutorial we want to describe a code that helps you in understanding the coedeof Illegal start  type, for this we have a class name Illegalstartoftype.Inside this main method we have a try block that  include -

1)InputStreamReader-An InputStreamReader is the way to convert from byte stream into character stream.

2)Buffered Reader- This used to read a text from a input stream to provide you efficient mean of reading character.

3)System.out.println-This is used to display the result on the command prompt

we have taken a variable string that is used to store the number entered on your command prompt and read the number from it by calling a readline method( ) . The for loop is  used  to check the condition if the value of  i<j ,the system.out.print display the value of i in the try block. Otherwise the try block is caught by subsequent catch block to handle the error in the try block.

Illegalstartoftype.java


import java.io.*;

public class Illegalstartoftype {

  public static void main(String args[]) throws Exception {
 try{
  InputStreamReader inputStreamReader = new InputStreamReader(System.in);
  BufferedReader br = new BufferedReader(inputStreamReader);

  System.out.println("Enter a number : ");
  String num = br.readLine();

  int j = Integer.valueOf(num);
  for (int i = 0; i < j; i++) {
  System.out.println(i);
  }}
  }catch(Exception ex){
 System.out.println(ex)
  }
}


On Compilation the code show an illegal start of type error.
Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
/home/girish/NetBeansProjects/errors/src/
Illegalstartoftype.java:
7'try' without 'catch' o'finally'
 

try{
/home/girish/NetBeansProjects/errors/src/
Illegalstartoftype.java:
18: illegal start of type


  }catch(Exception ex){
errors
BUILD FAILED (total time: seconds)


To solve this error check the closing braces of try after that only start catch

Download code