Can I use uncaughtexceptionhandler to ignore the exception and move forward?
If I can, how to write that handler?
for example:
try{
//something
}catch(exception e){
//something
}
//AND WHEN SOME UNCAUGHT EXCEPTION APPEAR, IGNORE EXCEPTION AND MOVE TO NEXT STEP
try{
//something else
}catch(exception e){
//something else
}
thank you for your attention
You can use the try .. (catch) .. finally construct to execute code even if an exception was thrown but not handled.
try {
// do things that can throw an exception
} finally {
// this block is !always! executed. Does not matter whether you
// "return"ed, an exception was thrown or everything was just fine.
}
if you add a catch in there then execution order is try up to the point where the exception happened, then the first appropriate catch block, then the finally block.
If you want to execute code in the finally block only if there was no exception, set some flag as the last operation of the try block.
boolean doFinally = true;
try {
// flag stays "true" if something throws here
doFinally = false;
} finally {
if (doFinally) {
// do something
}
}
Note: if you don't catch an otherwise uncaught exception somewhere your app will still crash after it has exectued the code in finally.
Related
I am using Glide v4 and it is not always can get bitmap from Url. Sometimes it works and Sometimes it does not work and throw an exception. I don't know why. this is the Exception:java.lang.IllegalArgumentException: You must call this method on a background thread and this is my code:
try {
bitmap=Glide.with(mContext.getApplicationContext())
.asBitmap().load(icon).fitCenter()
.circleCrop().submit().get();
} catch (Exception e) {
bitmap= BitmapFactory.decodeResource(mContext.getResources(),
R.drawable.ic_default_user_image);
}
I am facing another problem with Glide, this is the issue I made on Glide Github : https://github.com/bumptech/glide/issues/3590
java.lang.IllegalArgumentException: You must call this method on a background thread
The exception is very clear . you can't run your code that load a img on the main thread(which is UI Thread). This link may solve ur problem.
The exception says - "You must call this method on a background thread", for example :
Thread mThread = new Thread(new Runnable() {
#Override
public void run() {
try {
//Put your code that you want to run in here
} catch (Exception e) {
e.printStackTrace();
}
}
});
mThread.start
I am trying to save data into a Firebase RealtimeDatabase.
The process of saving is fine and works just well, but I was curious about an idea I had: To force the .setValue operation into a synchronous structure. I know, that that way isn't the best one, but I wanted to try it regardless of that fact.
So I came up with this code:
Thread t1 = new Thread(){
public void run(){
try {
Tasks.await(databaseReference.child("someChild").setValue(someObject));
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished");
}
In theory, that code snippet is intended to first set the value in the Database, then wait for the completion of that (through the Tasks.await()), and then print out **after that* the line "finished".
I wrapped the Tasks.await()-command in a second thread because when I don't, I get an exception which says something like "Tasks.await() must not be called in the main application thread".
But when I run the whole thing, it just gets stuck at t1.join();.
When I delete the t1.join();-statement, everything works just fine, but the second Thread (t1) isn't finished before the "finished"-output is there. I know why this is like that, but I am nontheless interested in a solution to that problem.
I am new in Android (Java) coding . I have a small game program downloaded. In that to terminate the thread , have the code
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
thread.setRunning(false);
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
retry = false;
}
}
Here the code author says if catch the exception , the code will go to try part not to the statement retry = false;
I could not believe that. please advise the flow is correct.
Please advise
Thanks
Anes
The loop will be run once. No matter if an Exception is thrown since retry will always be set to false after the first run. If you wanted the loop to continue until no Exception is thrown, you could place the retry = false; directly after the thread.join(); in the try block so it's only being called when you don't get to the catch block.
when use this code give me this error Unreachable catch block for IOException. This exception is never thrown from the try statement body
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakfast);
ListView lst=(ListView) findViewById(R.id.listView1);
try
{
Load_Database();
}
catch (IOException e)
{
throw new RuntimeException(e);
}
The catch block is only needed, if the function Load_Database() throws an IOException - since this apparently is not the case (I guess, without seeing the implementation, that Load_Database() handles the exception by itself), you should be safe removing the the whole try-catch block and simply call Load_Database()
If in your load_database() method you are doing connection creation or any other DB related stuff then change IOException to SQLException.
public void get(View view){
try {
asPt = new ProgressTask().execute(null,null,null);
Log.d("Watcher","Get finished");
}
catch (Exception e) {
e.printStackTrace();
Log.e("Watcher","Get Exception");
}
}
When I cancel(Boolean) the AsyncTask asPt the Line "Get finished" is never printed.
Why? It also doesn't catch an Exception in this method.
Remember cancel does nothing so you need to implement it yourself, see link: Android - Cancel AsyncTask Forcefully