1.
public class file3
{
    public static void main(String[] args)throws Exception 
    {
        File f=new File("d:\\filetest\\file1.txt");
        FileInputStream fis=new FileInputStream(f);
        char ch;
        for(int i=0;i<f.length();i++)
        {
            ch=(char)fis.read();
            System.out.print(ch);
        }
        fis.close();
    }
}
这里的throw Exception 是干什么用的?
2.
public class file2
{
    public static void main(String[] args)
    {
        file1 f=new file1();
        File f1=new File("d:\\filetest","1.txt");
        File f2=new File("d:\\filetest","2.txt");
        File f3=new File("d:\\filetest","3.txt");
        File f4=new File("d:\\filetest","4.txt");
        File f5=new File("d:\\filetest","5.txt");
        File f6=new File("d:\\filetest","6.txt");
        File f7=new File("d:\\filetest","7.txt");
        File f8=new File("d:\\filetest");
        try
        {
            f1.createNewFile();
            f2.createNewFile();
            f3.createNewFile();
            f4.createNewFile();
            f5.createNewFile();
            f6.createNewFile();
            f7.createNewFile();
        }
        catch(Exception e){}
        File[] ff=f8.listFiles();
        for(int i=0;i<ff.length;i++)
        {
            System.out.println("文件名称为:"+ff[i]);
        }
    }
这里为什么要加
try
{}
catch (exception e){}

解决方案 »

  1.   

    1.throw Exception 是抛异常,你还学异常吧? 学了后就明白了 
    比如这两句File f=new File("d:\\filetest\\file1.txt"); 
            FileInputStream fis=new FileInputStream(f); 
    都有可能出现异常 你可以查看API
    2.还是跟异常有关 这里不是抛,而是捕捉异常,try{}里面是可能出现异常的错误,catch(){}是出了异常后如何解决楼主还是先把异常这张看下吧
      

  2.   

    第一个是抛出异常,在进行IO流操作的时候都要进行异常处理,第二个也是对异常的处理。
    个人感觉catch (exception e){}不是一个好的写法,我们对异常进行处理就是希望在程序没有按照正常进行运
    行的时候进行错误的捕获,这个可以捕获的所有的错误。那时候我们可能分不出那是什么错误。
      

  3.   

    1.如果用户自定义其他的类的某个方法有其特殊的逻辑要求,可能抛出抛出自定义异常,所以在方法生命中抛出throws异常。
    2.try{
          methodX();//该方法可能抛出异常XxxxException
          methodX();//该方法可能抛出异常YyyyException
          ....;
    } catch(XxxxException){
         异常处理语句;//通常显示异常提示信息
    }catch{
         catch(YyyyException)({
         异常处理语句;
    }
    ....
      finaly{
         最终处理;//通常用于资源清理工作,如关闭文件,关闭链接等。
    }
    (1)如果没有产生异常,则执行完try块中的语句后,执行fianly块中的语句;
    (1)如果有产生异常,则try块中产生异常对象的方法调用之后的语句不再执行,而执行匹配的catch块,进行异常处理,最后执行finaly块中的语句。
      

  4.   

    恩,我手里是一本《零基础学Java》,自学的,还没碰到异常的相关章节,谢谢大家!
      

  5.   

    有异常就要处理异常
    正常的处理异常方式就是try{}catch块和throw