aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception Question?" Watch "Exception Question?" New topic
Author

Exception Question?

RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello Folks,

I think the following code is appropriate way to throw an exception.
But the wizlab says this is creating object, but not throw an exception.
I couldn't understand about the explained about this. Pls explain to me.

Thanks, Raghu.K


Exception e = new MalformedURLException("Invalid URL");

if (!myUrl.isValid()) // myURL is a URL object
{
thorw e;
}
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
If "myUrl.isValid()" returns true, then there is no an exception thrown.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Why create a new instance of this Exception unless you know there is a use for it? The preferred approach is to create the instance at the point of throwing it...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
RAGU KANNAN
Ranch Hand

Joined: Dec 16, 2005
Posts: 103
Hello,

Sorry for the confusion about my previous question here is correct question.

The answer for the following question is �B�, but the couldn�t understand about the answer �A�, Can you explain what is different between �A� and �B� or what they trying to explaining about answer �A�.

Thanks, Raghu.K


Question :-
Which one of the following fragments shows the most appropriate way to throw an exception? Assume that any undeclared variables have been appropriately declared elsewhere and are in scope and have meaningful values.

A) Exception e = new MalformedURLException("Invalid URL");
if(!myURL.isValid()) // myURL is a URL object
{
throw e;
}

B) if(!myURL.isValid())
{
throw new MalformedURLException("URL" + myURL.getName() + "is not valid");
}
C) if(!myURL.isValid())
{
throw IOException;
}
D) if(!myURL.isValid())
{
throw "Invalid URL";
}
E) if(!myURL.isValid())
{
throw new IOException;
}

Answer:-
Choice B is correct. A is incorrect as it misleads us into thinking that the exception occurred at line 1, which is where the exception object was created. C is illegal as it doesn; t throw an object but a class which is illegal. D is illegal as it throws a String and not an object. E is fine but not as good as B because it doesn't clarify the cause of the exception. B is the most appropriate way as it not only reports the exact line of exception but also explains properly the cause of exception.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Mr. marc has already pointed out that and gave the reply.

- Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by RAGU KANNAN:
...A is incorrect as it misleads us into thinking that the exception occurred at line 1, which is where the exception object was created...

This is another reason to create the instance on the line it's thrown: The stack trace ( printStackTrace() ) will indicate the line number on which the exception was created, so creating the Exception on the line it's thrown gives you a better indication of where the problem started.
 
jQuery in Action, 2nd edition
 
subject: Exception Question?