nikmin
nikmin

Reputation: 1843

android get exception type

I have an asynctask that's trying to send a mail in background. If the password or something else is not set up properly it throws exception. Depending of what exception it throws I need to show different text on toast. How can I get what exception was thrown so I can deal with this.

Upvotes: 0

Views: 4339

Answers (3)

Avadhani Y
Avadhani Y

Reputation: 7636

Please place the below code to show the Toast message whenever u caught an Exception:

  try{
  // Place the code which you think, will get an Exception
  } catch(Exception e) { 
   // show Toast as below:
    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
 }

Upvotes: 0

Renjith
Renjith

Reputation: 5803

First catch the exception using try catch block.Then you can show toast on each Exception Block. You can catch specific exceptions and do according to your needs.

For e.g..:

    try{
          //your necessary codes
     } catch(Exception e)
          // show Toast 
     } catch(IOException e1) { 

     }catch (NullPointerException e2)

     }catch(RunTimeException e3) {

     }

etc.. like this...!!

Upvotes: 1

Zartch
Zartch

Reputation: 136

You can catch a exception from more specific to more generic.

                try {

            }
            catch (IOException ex)
            {
                .....
            }
            catch (Exception ex) {
                Log.e( TAG , "Error" + ex.getMessage());
                                    Toast.maketext(.......)
            }

You can take a look at exceptions here: http://www.roseindia.net/java/java-exception/index.shtml You can pick up one or define your own.

Upvotes: 2

Related Questions