-1

Following is my piece of code:\

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();
        }

Load project file throws an exception which i recieve in Map class. This exception contains message: Java.lang.exception: java.lang.exception: file not found.

I want to show only "File not found" message. so how should i get this message out from an exception?

Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
Muneem Habib
  • 1,046
  • 4
  • 18
  • 49
  • Have a look at this, its exactly what you looking for http://stackoverflow.com/questions/9017820/exception-getmessage-output-without-class-name – Rachit Mishra Aug 26 '13 at 12:05

2 Answers2

0

Catch all types of exceptions instead of base Exception:

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);
}

Shortly: Use different class for different exception to show correct information rahter than using exception message.

Ari
  • 3,101
  • 2
  • 27
  • 49
  • i have cathed these exception in loadproject file exception – Muneem Habib Aug 26 '13 at 12:05
  • @MuneemHabib I don't understand. You are not catching it in code snippet you provided. If you are catching them inside `m_Renderer.m_Project.load_file()` and throwing Exception instead you shouldn't. – Ari Aug 26 '13 at 12:08
0

Just catch the FileNotFoundException instead of an Exception.

For example:

try {
      //whatever
} catch (FileNotFoundException e) {
      System.out.println("File not found");
}