Results 1 to 13 of 13
  1. #1
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default Exceptions & More

    So, I currently have 2 problems:
    1) In the following code, I get a "MyClass cannot be resolved to a type" error. This happens after I delete "MyClass.java". My exception is supposed to skip this sort of thing and move on:
    Java Code:
    	// ClassNotFound exception
    	public static void ClassNotFound(){
    		try{
    			MyClass myclass = new MyClass();}
    		catch (NoClassDefFoundError e){
    			System.out.println(e);}}
    2) In the following 2 methods, I have to "rethrow this exception":
    Java Code:
    	// ArrayIndexOutOfBounds exception
    	public static void ArrayIndexOutOfBounds(int size, int index){
    		try{
    			double[] myArray = new double[size];
    			double last = myArray[index];}
    		catch (ArrayIndexOutOfBoundsException e){
    			throw e;}}
    	
    	// Arithmetic exception
    	public static double Arithmetic(int dividend, int divisor){
    		try{
    			double result = dividend/divisor;
    			return result;}
    		catch (ArithmeticException e){
    			throw e;}}
    If I rethrow it like I put in those 2 methods, then I'll get the original exception error again, and it won't move on through the rest of the program.

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

    Default

    Why are you trying to throw the exception from the catch block? What purpose is this supposed to serve? You either catch the exception and deal with it in the catch block, or you don't use try/catch but rather have the method or constructor throw the exception, and then catch it further up the call chain. What you're trying to do seems self-defeating to me (unless I'm missing something here).

    Edit: Your first problem I believe is a compile error, not an exception, which exceptions won't fix.

  3. #3
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Why are you trying to throw the exception from the catch block? What purpose is this supposed to serve? You either catch the exception and deal with it in the catch block, or you don't use try/catch but rather have the method or constructor throw the exception, and then catch it further up the call chain. What you're trying to do seems self-defeating to me (unless I'm missing something here).
    According to the examples from my book, all of the throwing happens inside of catch... I don't know what it's supposed to do. Just following instructions from my assignment.

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

    Default

    Can you show me one of these examples? Again, maybe I'm missing something obvious, because I'm not claiming to be a pro at Java.

  5. #5
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    Here's one example. I'm referring to the 4th to the last line:
    Java Code:
    import java.util.*;
    
    import org.omg.CORBA.PUBLIC_MEMBER;
    
    import com.sun.javadoc.ThrowsTag;
    import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion.Static;
    
    public class RethrowExceptionExmpl
    {
    	static Scanner console = new Scanner(System.in);
    	
    	public static void main(String[] args)
    	{
    		int number;
    		
    		try
    		{
    			number = getNumber();
    			System.out.println("Line 5: number = "+number);
    		}
    		catch (InputMismatchException imeRef)
    		{
    			System.out.println("Line 7: Exception "+imeRef.toString());
    		}
    		public Static intStatic getNumber() throws InputMismatchException
    		{
    			int num;
    			
    			try
    			{
    				System.out.println("Line 11: Enter an "+ "integer: ");
    				num = console.nextInt();
    				System.out.println();
    				
    				return num;
    			}
    			catch (InputMismatchException imeRef)
    			{
    				throw imeRef;
    			}
    		}
    	}
    }

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

    Default

    Sorry, but your book is confusing me, because for the life of me, I don't see how this:
    Java Code:
    		public Static intStatic getNumber() throws InputMismatchException
    		{
    			int num;
    			
    			try
    			{
    				System.out.println("Line 11: Enter an "+ "integer: ");
    				num = console.nextInt();
    				System.out.println();
    				
    				return num;
    			}
    			catch (InputMismatchException imeRef)
    			{
    				throw imeRef;
    			}
    		}

    is any better or different from this:
    Java Code:
    		public Static intStatic getNumber() throws InputMismatchException
    		{
    			int num;
    			
    			System.out.println("Line 11: Enter an "+ "integer: ");
    			num = console.nextInt();
    			System.out.println();
    				
    			return num;
    		}

    If anyone can explain this to me, I'd appreciate it.

  7. #7
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    I certainly can't explain it :).

    So any ideas for my problem? ;) Here's the instructions for number 2:


    Here's the instructions for the first one:


    Keep in mind that everything in try, I'm supposed to leave unchanged.

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

    Default

    I guess I can see re-throwing the exception if you want to do something about the exception within the method (and also perhaps have a finally block in the method), but also want the calling code to handle the exception as well, but what confused me is your book's example did neither of these. But I'm still waiting for someone smarter than me to verify or clarify this.

  9. #9
    besweeet is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    So, as for the rethrowing part, I guess I did what the assignment has asked, right? If that's the case, I wish I could get the first part figure out. It only gives me that error unless MyClass.java is created, but my exception is supposed to avoid requiring it (I guess).

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    I guess I can see re-throwing the exception if you want to do something about the exception within the method (and also perhaps have a finally block in the method), but also want the calling code to handle the exception as well, but what confused me is your book's example did neither of these. But I'm still waiting for someone smarter than me to verify or clarify this.
    Well, you don't need the catch if all you want to do is something in the finally, so even then it's a pointless exercise. So, clearly I am not smarter than you...:)

    Quote Originally Posted by besweeet View Post
    So, as for the rethrowing part, I guess I did what the assignment has asked, right? If that's the case, I wish I could get the first part figure out. It only gives me that error unless MyClass.java is created, but my exception is supposed to avoid requiring it (I guess).
    But, as Fubarable has pointed out, that error you are getting is at compile time, so all that code is pointless. If you want the exception to be thrown you need to do something like:
    Java Code:
    Class.forName("MyClass");

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    It's common in layered structures to require many layers to perform some action when one exception is thrown (more common with application exceptions).

    So one layer catches the exception and, say sends an email, then rethrows it so the upper layers receive it too and react accordingly.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default

    But that's not what's happening.
    They're simply rethrowing the exception.
    No processing involved.

  13. #13
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Conclusion: sometimes, the guys who write books should really be reading other guys' books instead.

Similar Threads

  1. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 09:38 AM
  2. Exceptions in thread
    By Goodwater in forum New To Java
    Replies: 4
    Last Post: 03-16-2009, 04:00 AM
  3. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 08:12 PM
  4. How to use chained exceptions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 03:50 PM
  5. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 07:47 PM

Posting Permissions

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