Home Java Java-get-example Java error illegal start of type
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java error illegal start of type

Advertisement
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

     

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
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 3, 2008

Related Tutorials

Ask Questions?    Discuss: Java error illegal start of type   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Rithvik Mandal
July 14, 2013
problem please help illegal start of type.....

import java.io.*; class mobile1 { public static void main()throws IOException; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); { System.out.println("CHOICE ANY ONE"); System.out.println("1.CALLING"); System.out.println("2.MESSEGING"); i=Integer.parseInt(br.readLine()); } switch(i) { case 1:BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER 10 DIGITS NUMBER"); int i = Integer.parseInt(br.readLine); system.out.println("calling now....."); system.out.println("IT COST YOU 2 MIN"); break; case 2:BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("ENTER text"); int i = Integer.parseInt(br.readLine); system.out.println("sending"); system.out.println("IT COST YOU 1 RUPPEES"); break; } } }
DMCA.com