Results 1 to 3 of 3
  1. #1
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Arrow Handling Exception - elementary question

    Hi everyone,

    I am new to java. I have some problems with exceptions.

    When I run this code:
    Java Code:
    package myTestingPackage;
    
    import java.lang.*;
    
    public class Test3 {
    	public static int divide(int i, int j){
    		/*if (j==0){
    			return -1;
    		}
    		if (i % j != 0){
    			return -1;
    		}*/
    		return i / j;
    	}
    	
    	public static void main (String[] args) {
    		int d = divide(8,0);
    		System.out.println(d);
    	}
    
    }
    I have this message
    Java Code:
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    	at myTestingPackage.Test3.divide(Test3.java:13)
    	at myTestingPackage.Test3.main(Test3.java:17)
    So I see that there is an ArithmeticException, so I change the code into this:
    Java Code:
    package myTestingPackage;
    
    import java.lang.*;
    
    public class Test3 {
    	public static int divide(int i, int j) throws ArithmeticException{
    		/*if (j==0){
    			return -1;
    		}
    		if (i % j != 0){
    			return -1;
    		}*/
    		return i / j;
    	}
    	
    	public static void main (String[] args) {
    		int d = divide(8,0);
    		System.out.println(d);
    	}
    
    }
    And I still have an error message
    Java Code:
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    	at myTestingPackage.Test3.divide(Test3.java:13)
    	at myTestingPackage.Test3.main(Test3.java:17)
    The question is why is there no difference in the error messages?

    Thank you!

  2. #2
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    4

    Default

    return j / i; will work for you.

    8/0 just can not work, try it in your microsoft calculator, but 0/8 = 0;

    I am not exactly sure, but did you also try using a Try and Catch Statement?
    Last edited by Sno; 05-06-2010 at 11:40 PM. Reason: Adding Specifics
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    ArithmeticException is a RuntimeException, and is unchecked. Therefore you are not required to add a throws declaration to your method. In any case, a throws declaration does not prevent the Exception from being thrown. It is simply a warning to developers who use your class that this method can throw that Exception, and that they should deal with it somehow.

    -Gary-

Similar Threads

  1. Exception Handling help
    By MZA in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 09:23 AM
  2. JAXP exception handling
    By jovenky in forum Advanced Java
    Replies: 0
    Last Post: 05-27-2008, 01:37 PM
  3. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  4. JDBC - Exception handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:00 PM
  5. Jstl Exception Handling
    By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-18-2007, 06:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •