Results 1 to 11 of 11
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default ways to use try, catch and finally in a method

    Hello,

    so here is my question. I am writing a class with a bunch of methods and I kept getting the ArithmeticException so then I realize that you can put it in your main class, which I did but I'd like to put in the class with the methods. Is this possible?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by bigsonny View Post
    so here is my question. I am writing a class with a bunch of methods and I kept getting the ArithmeticException so then I realize that you can put it in your main class, which I did but I'd like to put in the class with the methods. Is this possible?
    I'm confused by your question. Can you show code and clarify what you're trying to do and what isn't working?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: ways to use try, catch and finally in a method

    Yes. However some believe you should not catch an ArithmeticException but ensure it does not occur in the first place. For example if you want to perform a division use an if statement to check if denominator is zero.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by Junky View Post
    Yes. However some believe you should not catch an ArithmeticException but ensure it does not occur in the first place. For example if you want to perform a division use an if statement to check if denominator is zero.
    So does just checking if it is zero keep the exception from being thrown? Also, do I do it in the class where I declare my methods or do I do it in the main class?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: ways to use try, catch and finally in a method

    In what method you should catch an exception and where to throw it (if you have the choice) is a very under estimated design issue; methods that have 'throws Exception' in their signature is a clear symptom of that. You should only catch and handle an exception if your method knows how to handle it; this implies that an exception should be cought as close to where it's thrown (catching it in your main( ... ) method is generally a bad place). o.t.o.h. if you know a piece of code can throw an exception and you know how to handle it, why let it throw the exception in the first place?

    A division by zero is such a situation: you can test if a denominator is zero and you know what to do if it is, so test for it and handle it. Your method would be sort of a coward if it let the exception slip away to its caller (and it may not know how to handle it).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by Fubarable View Post
    I'm confused by your question. Can you show code and clarify what you're trying to do and what isn't working?
    Here is the original code which throws the error:
    Java Code:
    //Method to calculate Combinations
    public int getCombi(int wbMax, int rChoice) throws ArithmeticException{
    
    	
    	this.wbMax = wbMax; 
    	this.rChoice = rChoice;
    	
    		combi = getFacto(wbMax)/((getFacto(rChoice)*(getFacto(wbMax-rChoice))));
    	
    	return combi;
    	 
    }//End of Comb

  7. #7
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by JosAH View Post
    In what method you should catch an exception and where to throw it (if you have the choice) is a very under estimated design issue; methods that have 'throws Exception' in their signature is a clear symptom of that. You should only catch and handle an exception if your method knows how to handle it; this implies that an exception should be cought as close to where it's thrown (catching it in your main( ... ) method is generally a bad place). o.t.o.h. if you know a piece of code can throw an exception and you know how to handle it, why let it throw the exception in the first place?

    A division by zero is such a situation: you can test if a denominator is zero and you know what to do if it is, so test for it and handle it. Your method would be sort of a coward if it let the exception slip away to its caller (and it may not know how to handle it).

    kind regards,

    Jos
    Thanks Jos, this is a good response. This is the what. I need to work on the how.
    Last edited by bigsonny; 06-11-2013 at 10:45 PM.

  8. #8
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    This is my real code. First is the main class, then my stats class. i seriously don't understand why I get the arithmetic error. More so, can you show me a full example or tell me specifically how to write a method which handles the exception and still executes. I have searched extensively for a good example and I can't seem to find one.

    Java Code:
    import java.io.*;
    
    public class Sandbox {
    
    	public static void main(String[] args) {
    	
    		
    	Stats lot = new Stats (5,5);
    	
    
    
    	System.out.println(lot.getCombi(10,3));	
    
    
    
    }//end of main method
    }//end of class
    Other class

    Java Code:
       public class Stats {
       
       
          private int wbMax;
          private int rChoice;
          public int combi=1;//combination value
          private int iter=1; //holds iteration
       
       //Stats Constructor
          public Stats(int wbMax, int rChoice){
          
             this.wbMax = wbMax;
             this.rChoice = rChoice;
             
          
          }//End of constructor
       
       //Method to calculate factorial
          public int getFacto(int facto){
             int factox = 1;
          
             for (int iter = facto; iter >=1; iter--){
                factox = factox * facto;
                facto = --facto;
             
             	
             }//end of for loop
          
             return factox;
          }//end of getFacto() method
       
       //Method to calculate Combinations
          public int getCombi(int wbMax, int rChoice) throws ArithmeticException{
          
          
             this.wbMax = wbMax; 
             this.rChoice = rChoice;
          
             try {
                combi = getFacto(wbMax)/((getFacto(rChoice)*(getFacto(wbMax-rChoice))));
             }
                catch (ArithmeticException e) {
                   System.out.print ("caught");
                }
             finally{
                return combi;
             }
          
          }//End of Combi
       
       
       
       }//End of Stats Class

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    Never mind. I got it. Replace all of the int with double and it works. It was an integer division problem.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by bigsonny
    Hello Darryl,

    would you mind taking a look at my post and offering me some feedback please?

    I am stuck.

    Thanks,
    bigsonny
    Don't send PMs asking for technical help. That will have the opposite effect.

    Ask your questions on the forum and wait patiently till a member answers. We are all volunteers here, and a volunteer doesn't like to feel pressured.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: ways to use try, catch and finally in a method

    Quote Originally Posted by DarrylBurke View Post
    Don't send PMs asking for technical help. That will have the opposite effect.

    Ask your questions on the forum and wait patiently till a member answers. We are all volunteers here, and a volunteer doesn't like to feel pressured.

    db
    Sorry DB,

    I wasn't trying to make you feel pressured. I was stuck on that part of the problem and since you're one of the moderators and since you were active around that time, I was trying to see if you could catch you while you were online. I didn't write to you in a rude demanding way, I was just asking if you'd please help but Fair enough...I won't PM you anymore...

Similar Threads

  1. Replies: 2
    Last Post: 08-30-2012, 04:51 PM
  2. Replies: 3
    Last Post: 06-22-2012, 06:54 PM
  3. Try-catch-(finally?) on reading/writing to files
    By Torchi12 in forum New To Java
    Replies: 8
    Last Post: 11-29-2011, 01:42 PM
  4. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 10:54 AM
  5. Replies: 15
    Last Post: 09-29-2008, 07:18 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
  •