要throws异常的类,就必须捕获它 !!! 否则,系统崩溃了你都不知道怎么崩溃的.而且如果捕获异常的话,做适当处理,系统根本就不会崩溃了 ------------------------------------------------------
           我们还年轻牛奶会有的奶牛也会有的 
             可天天在 csdn 混这些会有吗 ??

解决方案 »

  1.   

    除了catch,还可以thrown给调用者。
      

  2.   

    import java.io.*;
    import java.util.*;class ReadMultiFile
    {
    public static void main(String args[])
    {
    try
    {
    //instance of FilterFile
    FileFilter fFilter = new FileFilter("c:/j2sdk1.4.1/bin","java");
    //create sequenceInputStream
    SequenceInputStream seqInput = new SequenceInputStream(fFilter);
    //create FileOutputStream
    FileOutputStream fOutput = new FileOutputStream("SourceSum");
    //char readed every time;
    int nContent;
    //read data from SequenceInputStream
    while((nContent = seqInput.read()) != 1)
    {
    //write data to file output
    fOutput.write(nContent);
    }
    //close SequenceInputStream
    seqInput.close();
    //close File Onput Stream
    fOutput.close();
    }
    catch(IOException e)
    {
    e.printStackTrace();
    }
    }
    }
    class Filter implements FilenameFilter
    {
    String extension;
    Filter(String extension)
    {
    this.extension = extension;
    }

    public boolean accept(File directory, String filename)
    {
    return filename.endsWith("."+extension);
    }
    }
    class FileFilter implements Enumeration
    {
    File directory;
    String extension;
    String strFileList[];
    Filter filter;
    int nCounter;
    //construction
    FileFilter(String strDirectoryName, String extension)
    {
    this.extension = extension;
    //create directory
    directory = new File(strDirectoryName);
    if(directory.isDirectory())
    {
    filter = new Filter(extension);
    strFileList = directory.list(filter);
    }
    else
    {
    //if not directory, get current file
    strFileList = new String[1];
    strFileList[0] = strDirectoryName;
    }
    nCounter = 0;
    }
    //judge whether have other elements
    public boolean hasMoreElements()
    {
    if(nCounter < strFileList.length)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    public Object nextElement()
    {

    //if have another element
    if(hasMoreElements())
    {
    nCounter++;
    //return the fileInputStream
    FileInputStream fip = new FileInputStream(strFileList[nCounter-1]);
    return fip;
    }
    else
    {
    return null;
    } }
    }
    在这个简单的例子里面哪些需要做异常处理呢?
      

  3.   

    我很菜,只要JBUILDER报错我就catch &throw
      

  4.   

    当你调用其他class的函数的时候,一定要看看这个函数的定义,如果定义了有可能扔出exception,那么在你的source中调用的地方要么用try catch把exception处理掉,要么在你自己写的函数也加一个throws Exception,将exception继续向外扔。
      

  5.   

    其实有些方法扔了些exception出来,你可能从没听说过。不过你不必去管,
    编译器会告诉你应该捕获哪些异常,否则就不会让你通过。
      

  6.   

    类似   tmpMethod() throws XXException; 的方法就需要你 try... catch..否则就不需要
      

  7.   

    unreported exception java.io.FileNotFoundException must be caught or declared to be thrown 
        FileInputStream fip = new FileInputStream(strFileList[nCounter-1]);1 error!
      

  8.   

    因为, FileInputStream的constructor, 肯定抛出了 FileNotFoundException
    public FileInputStream(String name)  throws FileNotFoundException只要 方法 后面有 throws X1Exception, X2Exception ,..
    你就必需使用 try...catch 捕捉, 否则编译不通过