Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

by zero exception

Hi,

public class Exceptions {

	public void test() throws NullPointerException {
		NullPointerException e = new NullPointerException("");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			//System.out.println("next");
			// } catch (NullPointerException e) {
			//Exceptions obj = new Exceptions();
			//obj.test();
			int a=5;
			int b=0;
			int c=a/b;

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		// catch (NullPointerException e) {
		catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		// System.out.println("hi");
	}

}

Open in new window


i see above code generating
/ by zero


i wonder where above message came from as i have not set any message in my code?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I wonder where above message came from<<
What above message?
Avatar of gudii9

ASKER

, and the message you get is the detail message for that exception
that is defined in API iteself by jdk?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

when i ran my program i did not get detailed like below

Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Exceptions.main(Exceptions.java:5)


but insteead i just got
/ by zero
please advise
I added some lines that hopefully explain.  
public class Exceptions {
	public static void main(String[] args) {
		try{
			int a = 5;
			int b = 0;
			int c = a/b;
		} catch(Exception e){
			System.out.println(" message is " + e.getMessage());
			System.out.println(" toString() is " + e.toString());
			System.out.println("--------------------------------------");
			e.printStackTrace();
		}
		
	}
}

Open in new window

When I run that, I get
 message is / by zero
 toString() is java.lang.ArithmeticException: / by zero
--------------------------------------
java.lang.ArithmeticException: / by zero
        at Exceptions.main(Exceptions.java:6)

Open in new window

I forgot to answer your question.
that is defined in API iteself by jdk?

Somewhere in the bowels of Java (hopefully an expert will tell us where) an ArithmeticException is constructed using the constructor
http://docs.oracle.com/javase/8/docs/api/java/lang/ArithmeticException.html#ArithmeticException-java.lang.String- 
I post it here.
public ArithmeticException(String s)
Constructs an ArithmeticException with the specified detail message.
Parameters:
s - the detail message.
So,  somewhere there is code like
throw new ArithmeticException("/ by zero");

Open in new window

Avatar of gudii9

ASKER

catch (Exception e) {
                  // TODO: handle exception
                  System.out.println(e.getMessage());

i just printed e.getMessage

not

e.printStackTrace();

which i usually used to do..
Avatar of gudii9

ASKER

public class ExceptionsMessage {

	public void test() throws NullPointerException {
		NullPointerException e = new NullPointerException("");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			//System.out.println("next");
			// } catch (NullPointerException e) {
			//Exceptions obj = new Exceptions();
			//obj.test();
			int a=5;
			int b=0;
			int c=a/b;

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		// catch (NullPointerException e) {
		catch (Exception e) {
			// TODO: handle exception
			//System.out.println(e.getMessage());
			//System.out.println(e.printStackTrace());
			e.printStackTrace();
		}
		// System.out.println("hi");
	}

Open in new window

above gave what i am looking output

java.lang.ArithmeticException: / by zero
      at com.mkyong.test.core.ExceptionsMessage.main(ExceptionsMessage.java:22)
Avatar of gudii9

ASKER

public class ExceptionsMessage {

	public void test() throws NullPointerException {
		NullPointerException e = new NullPointerException("");
		throw e;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// int arr[] = { 1, 2, 3, 4, 5 };

		try {
			// System.out.println(arr[5]);
			//System.out.println("next");
			// } catch (NullPointerException e) {
			//Exceptions obj = new Exceptions();
			//obj.test();
			int a=5;
			int b=0;
			int c=a/b;

		}
		// System.out.println("in between try and catch");
		// catch (ArrayIndexOutOfBoundsException e) {
		// catch (NullPointerException e) {
		catch (Exception e) {
			// TODO: handle exception
			//System.out.println(e.getMessage());
			System.out.println(e.printStackTrace());
			//e.printStackTrace();
		}
		// System.out.println("hi");
	}

}

Open in new window


why aboe gave below error at line 31?

The method println(boolean) in the type PrintStream is not applicable for the arguments (void)