I want to print the stack trace because at the moment I have this running.
} catch (IOException e) { throw new Error("Copying Failed"); }
And I have been told to print e.stacktrace();
How do I do this?
Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.
Example: Convert stack trace to a string In the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.
} catch (IOException e) { Log.e("YOUR_APP_LOG_TAG", "I got an error", e); }
And check the LogCat for the output.
An other method, very useful :
try { ... } catch (Exception e) { Log.e(APP_TAG, "STACKTRACE"); Log.e(APP_TAG, Log.getStackTraceString(e)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With