Results 1 to 8 of 8
  1. #1
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Question about handling an AssertionError

    I'm trying to handle a AssertionError and I am having trouble catching it. The program is terminating on the compiler error. This is a homework assignment, and I am only looking for any pointers that someone might have.

    We are instructed to use a class AssertionError and accept the failed int in its constructor. The code below is what I have.

    Java Code:
    public class Assignment_3_Part_5 {
    
    	
    	public static void main(String[] args) {
    		
    		int number = -5;
    		
    		try {
    			numberChecker(number);
    			System.out.println("The number " + number + " is valid.");
    		}catch (AssertionError ae){
    			throw new AssertionError(number);
    		}
    		
    	}
    	
    	private static void numberChecker(int number) throws AssertionError{
    		
    		assert(number >= 0) : "Assertion Error thrown";
    		
    	}
    }
    
    class AssertionError extends Error{
    	
    	public AssertionError(int detailMessage){
    		System.out.println("AssertionError handled. The number " 
    							+ detailMessage  + " cannot be negative");
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Question about handling an AssertionError

    Are you certain that assertions are enabled? If you are running from the command line, try using

    java -ea prog

    to run the program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: Question about handling an AssertionError

    Yes, I have Assertions enabled in eclipse. I do get a assertion error but I get the one generated by the compiler and my program terminates prematurely.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Question about handling an AssertionError

    I'm not certain what you mean when you say you get the one generated by the compiler. The program compiles just fine and compilers don't generate run time assertion errors. I use Eclipse and when I run it with assertions on I get the assertion error and nothing more. Can you show all the errors you are getting?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: Question about handling an AssertionError

    I'm trying generate the error through the AssertionError constructor and display that message when an assertion error is thrown. As of now, the code terminates as soon as the assertion is false. In some of my other exception assignments our instructor has expected us to recover from the exception and display a message through the a constructor. It seems to do what it is supposed to but I don't know why my instructor would want us to pass the bad value into The AssertionError class constructor if it never gets called through a catch.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,510
    Rep Power
    5

    Default Re: Question about handling an AssertionError

    As I said, I need to see the error messages.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: Question about handling an AssertionError

    This is the error message that I am getting:

    Exception in thread "main" java.lang.AssertionError: AssertionError thrown
    at Assignment_3_Part_5.numberChecker(Assignment_3_Par t_5.java:20)
    at Assignment_3_Part_5.main(Assignment_3_Part_5.java: 10)

    I know that is what is supposed to happen when a assertion error is thrown, but I want the error message to come from the AssertionError constructor. Is it not possible to catch this because it is technically an error and not an exception?

    Mike

  8. #8
    generaltso is offline Member
    Join Date
    May 2013
    Posts
    9
    Rep Power
    0

    Default Re: Question about handling an AssertionError

    Jim,

    Sorry about the goose chase. From what I've read, the second statement of assert passes the information to the AssertionError's constructor. I only assumed that I need to create an separate class and constructor to handle this, like I had with the custom exceptions that I had made before.

Similar Threads

  1. AssertionError?
    By Draymire in forum New To Java
    Replies: 11
    Last Post: 10-04-2012, 06:16 PM
  2. Question about exception handling - Help needed
    By gnarkill10 in forum New To Java
    Replies: 2
    Last Post: 08-17-2012, 10:40 AM
  3. Exception handling question
    By BenH in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2012, 11:13 PM
  4. Question about Exception handling
    By bigsonny in forum New To Java
    Replies: 26
    Last Post: 08-05-2011, 12:28 AM
  5. Handling Exception - elementary question
    By kracer in forum New To Java
    Replies: 2
    Last Post: 05-06-2010, 11:47 PM

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
  •