Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Question?

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If "myUrl.isValid()" returns true, then there is no an exception thrown.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. marc has already pointed out that and gave the reply.

- Naseem
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic