The Artima Developer Community
Sponsored Link

Java Buzz Forum
3 different ways to print exception message in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
3 different ways to print exception message in java Posted: Apr 25, 2016 5:49 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: 3 different ways to print exception message in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • In Java there are three ways to find the details of the exception .
  • They are 
  1. Using an object of java.lang.Exception
  2. Using public void printStackTrace() method
  3. Using public String getMessage() method.

1.Using an object of java.lang.Exception

  •  An object of Exception class prints the name of the exception and nature of the exception.


Write a Java program get detailed message details using exception class object



  1. package exceptions;
  2. public class ExceptionDetails {
  3.  
  4. /**
  5.  * @www.instanceofjava.com
  6.  **/
  7.  public static void main(String[] args) {
  8.  
  9. try {
  10.  
  11. int x=1/0;
  12.             
  13. } catch (Exception e) {
  14.             System.out.println(e);
  15. }
  16.  
  17. }
  18.  
  19. }

 Output:


  1. java.lang.ArithmeticException: / by zero

 2.Using  public void printStackTrace() method


  • This is the method which is defined in java.lang.Throwable class and it is inherited into java.lang.Error class and java.lang.Exception class
  • This method will display the name of the exception and nature of the message and line number where exception has occurred.

Write a simple java example program to print exception message to console using printStacktrace() method



  1. package exceptions;
  2. public class ExceptionDetails {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7. public static void main(String[] args) {

  8.  
  9.  try {
  10.           int a[]= new int[1];
  11.             a[1]=12
  12.             
  13. } catch (Exception e) {
  14.    e.printStackTrace();
  15.            
  16.  }
  17. }
  18.  
  19. }

 Output:


  1. java.lang.ArrayIndexOutOfBoundsException: 1
        at exceptions.ExceptionDetails.main(ExceptionDetails.java:13)

  3.Using public String getMessage() method

  •  This is also a method which is defined in java.lang.Throwable class and it is inherited in to both java.lanf.Error and java.lang.Exception classes.
  • This method will display the only exception message


Write a Java program print exception message using getMessage() method.


  1. package exceptions;
  2. public class ExceptionDetails {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      */
  7.     public static void main(String[] args) {

  8.  
  9.         try {
  10.             int x=1/0;
  11.             
  12.         } catch (Exception e) {
  13.             System.out.println(e.getMessage());
  14.         }
  15.     }
  16.  
  17. }

 Output:


  1.  / by zero

print exception message

Read: 3 different ways to print exception message in java

Topic: What is management work? Previous Topic   Next Topic Topic: Spring Security with Spring REST Web-service

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use