gudii9
asked on
by zero exception
Hi,
i see above code generating
/ by zero
i wonder where above message came from as i have not set any message in my code?
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");
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
, and the message you get is the detail message for that exceptionthat is defined in API iteself by jdk?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
when i ran my program i did not get detailed like below
Exception in thread "main" java.lang.ArithmeticExcept ion: / by zero
at Exceptions.main(Exceptions .java:5)
but insteead i just got
/ by zero
please advise
Exception in thread "main" java.lang.ArithmeticExcept
at Exceptions.main(Exceptions
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();
}
}
}
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)
I forgot to answer your question.
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.
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 ArithmeticExceptionSo, somewhere there is code like(String s)
Constructs an ArithmeticException with the specified detail message.
Parameters:
s - the detail message.
throw new ArithmeticException("/ by zero");
ASKER
catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMe ssage());
i just printed e.getMessage
not
e.printStackTrace();
which i usually used to do..
// TODO: handle exception
System.out.println(e.getMe
i just printed e.getMessage
not
e.printStackTrace();
which i usually used to do..
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");
}
above gave what i am looking outputjava.lang.ArithmeticExcept
at com.mkyong.test.core.Excep
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");
}
}
why aboe gave below error at line 31?
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
What above message?