簡體   English   中英

Java程序錯誤,提示應使用類,接口或枚舉

[英]Java Program Error saying class , interface or enum expected

import java.io.*;

            public class Test13
    {
            public static void main(String[] args) throws IOException 

    {

    FileInputStream fis1 = new FileInputStream("D:/abc.txt");

    FileInputStream fis2 = new FileInputStream("D:/xyz.txt");

    SequenceInputStream sis = new SequenceInputStream(fis1,fis2);

    int i;

    while((i = sis.read())!=-1)

    {

    System.out.println((char)i);

    }

    }
    }


    catch(Exception ex)

    {

     ex.printStackTrace();

    }

    }

    }

我想您嘗試過類似的方法。 我對異常處理做了一些介紹

import java.io.FileInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;

public class Test13 {

    //because all exceptions are already catched main will never throw one 
    public static void main(String[] args) throws IOException {
        try {
            //if an exception raises anywhere from here ...

            FileInputStream fis1 = new FileInputStream("D:/abc.txt");
            FileInputStream fis2 = new FileInputStream("D:/xyz.txt");
            SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

            int i;
            while ((i = sis.read()) != -1) {
                System.out.println((char) i);
            }

            //... to here ...

        } catch (Exception ex) {
            //this catch block code will be executed
            ex.printStackTrace();           
        }
    }

}
Below is the updated code and its working fine.

    import java.io.*;

    public class Test13

    {
        public static void main(String[] args) throws IOException {
            try {
                FileInputStream fis1 = new FileInputStream("D:/abc.txt");

                FileInputStream fis2 = new FileInputStream("D:/xyz.txt");

                SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

                int i;

                while ((i = sis.read()) != -1)

                {

                    System.out.println((char) i);

                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

你應該有

   try{
         //function which throws exceptions
      }
   catch( SpecificException e ) {
         // if a specific exception was thrown, handle it here
    }
   catch(Exception ex) {        
        // if a more general exception was thrown, handle it here
      }
    finally{
       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM