-1

以下は私のコードです:\

public void LoadProjectFile(String Filename) throws Exception
{
  //  m_Renderer.m_Project=new Project();
   refresh_project();
    m_Renderer.m_SelectedProjectPath =  Filename;

    try {
        m_Renderer.m_Project.load_file(Filename);

    }
    catch (Exception e)

    {
        //Exception a = e.getMessage();
        String a= e.getMessage();

        throw new Exception(a);
    }

    //AppFuncs.m_GisProject=m_Renderer.m_Project;

}



        try
        {

            Map.this.mGLView.LoadProjectFile(AppFuncs.g_path);
             Map.this.mGLView.requestRender();
    }
        catch (Exception e)
        {
            br=1;
             b=e.getMessage();
        }

プロジェクト ファイルをロードすると、Map クラスで受け取る例外がスローされます。This exception contains message: Java.lang.exception: java.lang.exception: file not found.

メッセージだけを表示したい"File not found"。では、例外からこのメッセージを取得するにはどうすればよいですか?

4

2 に答える 2

0

FileNotFoundExceptionの代わりに をキャッチするだけExceptionです。

例えば:

try {
      //whatever
} catch (FileNotFoundException e) {
      System.out.println("File not found");
}
于 2013-08-26T12:30:47.297 に答える
0

基本例外の代わりにすべてのタイプの例外をキャッチします。

try {
    m_Renderer.m_Project.load_file(Filename);

}
catch (FileNotFoundException e)
{
    String a= "File not found"
    throw new Exception(a);
}
catch (SomeOhterException e){ 
    String a= "Some other message"
    throw new Exception(a);
}
//at the end, but it shouldn't be necessary 
catch (Exception e){ 
    String a= "Something happend we don't know what"
    throw new Exception(a);
}

簡単に言うと、異なる例外に対して異なるクラスを使用して、例外メッセージを使用するよりも正しい情報を表示します。

于 2013-08-26T12:03:10.293 に答える