dcsimg
What is the best way to handle an exception in the constructor
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Song_Lin
Posted On:   Wednesday, April 10, 2002 06:35 PM

			
public class Test {
public Test(String url) {
java.net.URL = new java.net.URL(url);
}
///some other methods...
}


This is a simple example, but the idea is that the code inside the constructor will throw MalformedURLException, in the catch block, what will be the best and right way to handle
it. Rethrow it?


Thanks in advance..


Song

Re: What is the best way to handle an exception in the constructor

Posted By:   Stephen_Ostermiller  
Posted On:   Thursday, April 11, 2002 04:39 AM

You should either handle the exception or rethrow it. If your class will still work after the exception occurs and whoever called the class doesn't need to know about it, then handle it. Otherwise declare that it throws it.

public Test(String url) throws MalformedURLException {
java.net.URL = new java.net.URL(url);
}
About | Sitemap | Contact