Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Throwing exceptions

Options
  • 04-05-2019 9:18pm
    #1
    Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,045 Mod ✭✭✭✭


    Lets say you're throwing an exception in this piece of code

    public void setFirstName(String firstName){
    	  
    	  if(firstName.length() < 5){
    		 
    		  throw new IllegalArgumentException("NAME LENGTH IS TOO SHORT! ");
    	  }
    	  this.firstName = firstName;
      }
    

    Is it possible to catch it in your tester class with a try/catch ? so the the program doesn't completely crash and just displays message?


Comments

  • Registered Users Posts: 27,095 ✭✭✭✭GreeBo


    Lets say you're throwing an exception in this piece of code

    public void setFirstName(String firstName){
    	  
    	  if(firstName.length() < 5){
    		 
    		  throw new IllegalArgumentException("NAME LENGTH IS TOO SHORT! ");
    	  }
    	  this.firstName = firstName;
      }
    

    Is it possible to catch it in your tester class with a try/catch ? so the the program doesn't completely crash and just displays message?


    Do you mean in the calling class or literally in a unit test?

    If unit test you can expect/assert that an exception is thrown, down to the type of exception and the expected message.
    What unit test framework are you using?
    Example for junit https://www.baeldung.com/junit-assert-exception


  • Registered Users Posts: 1,461 ✭✭✭Anesthetize


    The real question here is why are you checking the name length and throwing this Exception in the first place? And don't say because the specification told you to.

    I'd be very upset here if my name was John.


  • Registered Users Posts: 6,236 ✭✭✭Idleater


    The real question here is why are you checking the name length and throwing this Exception in the first place? And don't say because the specification told you to.

    I'd be very upset here if my name was John.

    Agreed.

    Exceptions controlling program flow is an anti-pattern. Not that you can't do it, you most certainly can, neither that in some circumstances you should do it, but in general I'd suggest that exceptions be used where something will go wrong rather than something that shouldn't be (and possibly detected earlier in the program flow).

    If the method is the first piece of code at the user input - yes, except that for business reason X the following input parameters are invalid. If the method is just before an insert into a DB, then why did it get that far, and will the program fault if that is run.

    https://softwareengineering.stackexchange.com/questions/189222/are-exceptions-as-control-flow-considered-a-serious-antipattern-if-so-why


  • Moderators, Computer Games Moderators, Social & Fun Moderators Posts: 80,045 Mod ✭✭✭✭Sephiroth_dude


    GreeBo wrote: »
    Do you mean in the calling class or literally in a unit test?

    If unit test you can expect/assert that an exception is thrown, down to the type of exception and the expected message.
    What unit test framework are you using?
    Example for junit https://www.baeldung.com/junit-assert-exception

    Cheers.

    Yes I mean the calling class

    I thought the below might work but I was wrong
    public class Car_Tester{
       public static void main (String[] args){
        
          CarType1 c1 = new CarType1("B.M.W","Series 7",100,51000,true);
          System.out.println(c1.toString());
          c1.move();
          
          try{
            setPrice(int price);
          }
          catch(IllegalArgumentException e)
          {
            System.out.println(" error");
          }
                
       }
    

    Idleater wrote: »


    If the method is the first piece of code at the user input - yes,

    This would be the case, I understand what you are saying but I am just interested in seeing how it would be done.


  • Moderators, Business & Finance Moderators Posts: 10,041 Mod ✭✭✭✭Jim2007


    The real question here is why are you checking the name length and throwing this Exception in the first place? And don't say because the specification told you to.

    I'd be very upset here if my name was John.

    No it is completely irrelevant to the question being asked. Actually.


  • Advertisement
  • Registered Users Posts: 27,095 ✭✭✭✭GreeBo


    Cheers.

    Yes I mean the calling class

    I thought the below might work but I was wrong
    public class Car_Tester{
       public static void main (String[] args){
        
          CarType1 c1 = new CarType1("B.M.W","Series 7",100,51000,true);
          System.out.println(c1.toString());
          c1.move();
          
          try{
            setPrice(int price);
          }
          catch(IllegalArgumentException e)
          {
            System.out.println(" error");
          }
                
       }
    




    This would be the case, I understand what you are saying but I am just interested in seeing how it would be done.

    Is it not compiling or not doing what you expect when you run it?
    Looks like it wont compile as "setPrice(int price);" is sort of a mixture of calling a method called setPrice and defining it.
    If you are calling it you dont pass the type (int), thats in the method signature (definition) like your setFirstName (String name) example.

    So it should be
    c1.setPrice(123)
    

    which, assuming setPrice throws an IllegalArgumentException if 123 is invalid, will do what you expect and print "error".

    I know its know what you asked, and that you can achieve what you are trying to by just using a main, but you should really take a look at basic unit tests, it makes life a lot easier in the long run.


  • Registered Users Posts: 359 ✭✭SharpCoder


    Cheers.

    Yes I mean the calling class

    I thought the below might work but I was wrong
    public class Car_Tester{
       public static void main (String[] args){
        
          CarType1 c1 = new CarType1("B.M.W","Series 7",100,51000,true);
          System.out.println(c1.toString());
          c1.move();
          
          try{
            setPrice(int price);
          }
          catch(IllegalArgumentException e)
          {
            System.out.println(" error");
          }
                
       }
    




    This would be the case, I understand what you are saying but I am just interested in seeing how it would be done.

    You're defining e as Exception, so you can use it generically.

    like..
    public void test(int num){
    try {
       num2 = num / 0;
       System.out.println(num2);
    }
    catch(exception e){ Messagebox.Show(e.message);  
    }
    

    however you're calling a method with a type when you shouldn't in this case.

    setPrice(int price); should be in your class, usually under your constructors and getters and setters, not in your main, you should be using setPrice(whatever value);
    public class Car_Tester{
       public static void main (String[] args){
        
          CarType1 c1 = new CarType1("B.M.W","Series 7",100,51000,true);
          System.out.println(c1.toString());
          c1.move();
          
          try{
            setPrice(int price);
          }
          catch(IllegalArgumentException e)
          {
            System.out.println(" error");
          }
                
       }
    


Advertisement