How to get message from Exception? - android

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?

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.

Just catch the FileNotFoundException instead of an Exception.
For example:
try {
//whatever
} catch (FileNotFoundException e) {
System.out.println("File not found");
}

Related

SecurityException with PdfRenderer, comes with password protected pdfs, and than repeats even with normal pdfs

If trying to open Password Protected PDF with PdfRenderer API, gives SecurityException and handled accordingly, inside catch block and thenonDestroy basic clean up is done, and comes back to home activity and then navigating a simple non-protected PDF than again the same exception occurs.
Please note, this happens only once any protected file got opened.
Refer below code:
#Override
protected PdfRenderer doInBackground(Uri... uri) {
Uri uriToProcess = uri[0];
try {
contentResolver=getContentResolver();
parcelFileDescriptor = contentResolver.openFileDescriptor(uriToProcess, "r");
if(parcelFileDescriptor!=null && mPdfRenderer==null) {
mPdfRenderer = new PdfRenderer(parcelFileDescriptor);
}
} catch (FileNotFoundException e) {
exceptionMsg="Sorry! No such file or directory found";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ FNFException", e.toString());
} catch (IOException e) {
exceptionMsg="Sorry! Something went wrong with IO";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ IOException", e.toString());
} catch (SecurityException e) {
if (parcelFileDescriptor!=null) {
try {
parcelFileDescriptor.close();
parcelFileDescriptor = null;
contentResolver=null;
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (mPdfRenderer!=null){
mPdfRenderer.close();
mPdfRenderer=null;
}
exceptionMsg="Password protected file, This can't be opened";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ SecurityException", e.toString());
} catch (Exception e) {
exceptionMsg="Sorry! Something went wrong.";
handleExceptionInUI(exceptionMsg, progressDialog);
Log.e("$$$$ EXCEPTION", e.toString());
}
return mPdfRenderer;
}
Any help cordially appreciated.
I had the same problem with my app. The way I solved it was using an https://github.com/TomRoush/PdfBox-Android and load the document and check for password protection. After no InvalidPasswordException has been thrown the file can be safely loaded with the PdfRenderer.

IO exception even after including try catch block

The following piece of code is creating error
Activity context;
context = new Activity();
try {
InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
//SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());
}catch (FileNotFoundException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
Error:
Error:(255, 87) error: unreported exception IOException; must be caught or declared to be thrown
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Even after I included the try,catch blocks why is it creating an error?
Change your code like this :
BEFORE:
Activity context;
context = new Activity();
try {
InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
//SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());
}catch (FileNotFoundException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
AFTER:
Activity context;
context = new Activity();
try {
InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
//SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());
}catch (IOException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
Try this, IOException is the parent of FileNotFoundException. So catching the IOException will ensure you catch any exception that is a subclass of IOException.
Activity context;
context = new Activity();
try {
InputStream configAsset = context.getApplicationContext().getAssets().open("MyConfig.cfg");
//SimpleSpkDetSystem alizeSystem = new SimpleSpkDetSystem(configAsset, context.getApplicationContext().getFilesDir().getPath());
}catch (IOException e) {
Log.e(LOG_TAG, "File not found for recording ", e);
}
You should include the following to avoid the crash,
try {
InputStream configAsset =getApplication().getAssets().open("MyConfig.cfg");
}catch (FileNotFoundException e) {
Log.e("ddsa", "File not found for recording ", e);
} catch (IOException e) {
Log.e("ddsa", "IOException ", e);
e.printStackTrace();
}
Also the context you set not work well for me that's why getApplication()

Creating new Signer throws XadesProfileResolutionException [Android Xades4j]

I have simple method getSigner which return new Signer (XadesSigner object).
The same method works on simple console java application but in Android throws XadesProfileResolutiionException.
Anyone help me?
private XadesSigner getSigner(String pfxPath, String password) throws SigningKeyException {
try {
KeyingDataProvider keyingProvider = getKeyingDataProvider(pfxPath, password);
XadesSigningProfile p = new XadesBesSigningProfile(keyingProvider);
return p.newSigner();
} catch (KeyStoreException ex) {
throw new SigningKeyException("Keystore Problem", ex);
} catch (SigningCertChainException ex) {
throw new SigningKeyException("Signer Cert Chain Problem", ex);
} catch (UnexpectedJCAException ex) {
throw new SigningKeyException("JCA Problem", ex);
} catch (XadesProfileResolutionException ex) {
throw new SigningKeyException("XadesProfileResolutionException Problem", ex);
}
}

Unable to set path in jaudiotagger

I'm trying to edit tags of audio files and for that I'm using JaudioTagger. But a problem exists and I can't figure it out.
I'm getting this error:
java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-oj.jar)
And my code for getting tags of audio files :
File path = new File(songInfoModel.getData());
Log.e("Path: ", path.toString());
try {
AudioFile f = AudioFileIO.read(path);
Tag tag = f.getTag();
String artist = tag.getFirst(FieldKey.ARTIST);
tag.getFirst(FieldKey.ALBUM);
tag.getFirst(FieldKey.TITLE);
tag.getFirst(FieldKey.COMMENT);
tag.getFirst(FieldKey.YEAR);
tag.getFirst(FieldKey.TRACK);
tag.getFirst(FieldKey.DISC_NO);
tag.getFirst(FieldKey.COMPOSER);
tag.getFirst(FieldKey.ARTIST_SORT);
Log.e("Artist: ",artist);
} catch (CannotReadException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TagException e) {
e.printStackTrace();
} catch (ReadOnlyFileException e) {
e.printStackTrace();
} catch (InvalidAudioFrameException e) {
e.printStackTrace();
}

BluetoothDevice getmethod NoSuchMethodException

I am creating new connection class for Bluetooth device on Android Studio.I cant figure out why thrown exception in design time.
public ConnectingThread(BluetoothDevice device,MainActivity activity,BluetoothAdapter adapter) {
mainActivity=activity;
bluetoothAdapter=adapter;
BluetoothSocket temp = null;
bluetoothDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
temp = (BluetoothSocket)bluetoothDevice.getClass().getMethod("createRfcommSocket",int.class).invoke(bluetoothDevice,1);
} catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = temp;
}
The line:
temp = (BluetoothSocket)bluetoothDevice.getClass().getMethod("createRfcommSocket",int.class).invoke(bluetoothDevice,1);
has the potential to throw a NoSuchMethodException. You do not have a catch block to handle this exception. So you have to add another catch block under your existing catch block as follows:
catch(NoSuchMethodException e){
//Insert code here
}
Also, that line of code will later throw the following exceptions so it is best to handle them as well: IllegalAccessException, and InvocationTargetException. Thus, your try-catch blocks should be as follows:
try {
temp = (BluetoothSocket)bluetoothDevice.getClass().getMethod("createRfcommSocket",int.class).invoke(bluetoothDevice,1);
} catch (IOException e) {
e.printStackTrace();
}
catch(NoSuchMethodException ex){
//Insert code here
}
catch(IllegalAccessException e){
//Insert code here
}
catch(InvocationTargetException e){
//Insert code here
}
Alternatively, you could handle every single exception with the general Exception class. In this case, your code should look something like this:
try {
temp = (BluetoothSocket)bluetoothDevice.getClass().getMethod("createRfcommSocket",int.class).invoke(bluetoothDevice,1);
} catch (Exception e) {
//Enter code here
}

Categories

Resources