URI Java URI Syntax Exception - java

I am trying to browse a uri that is more indepth than just the .com domain here's the code. But its claiming there's a syntax exception and when i throw it it still doesn't work.
import java.awt.Desktop;
import java.net.URI;
class Gui {
public static void main(String[] args) throws Exception {
URI GoogleplusURL = new URI("https://plus.google.com//u//0//115793082536946778715//posts");
Desktop.getDesktop().browse(GoogleplusURL);
}
}

There is no problem with your code.
Following format of code will help you understand the issue. Your code can throw two type of exception
URISyntaxException -- When you use invalid url syntax
Example: replace your url with https://plus.google.com/u/0/115793082536946778715/^posts
IOException -- This will occur when url is not valid
public static void main(String[] args) {
URI GoogleplusURL;
try {
GoogleplusURL = new URI("https://plus.google.com/u/0/115793082536946778715/posts");
Desktop.getDesktop().browse(GoogleplusURL);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In case you worrying about url is not working ? try to change your url
From: https://plus.google.com//u//0//115793082536946778715//posts
To: https://plus.google.com/u/0/115793082536946778715/posts

Related

Java exception handling with instantiate Exception superclass and IOException subclass

First I create a class called OrderHandler.java. I declared an instance of superclass Exception and an instance of subclass IOException in a main method.
Now the question is to show a compilation error when you try catching the superclass exception type before the subclass exception type. What should I do? Need I create some methods to show the path? Or do I need to instantiate the OrderHandler as well?
Thanks
import java.io.IOException;
public class OrderHandler {
public static void main(String[] args) {
// TODO Auto-generated method stub
Exception A = new Exception();
IOException B = new IOException();
}
}
You're not catching any exceptions in your sample code. I think you mean this:
try {
// do some stuff
} catch (Exception ex) {
// report a general exception
} catch (IOException ex) {
// report an IO exception
}
This isn't going to do what you want it to do. You need to catch more specific exceptions first, otherwise the IOException block will never execute. The correct way to do this is:
try {
// do some stuff
} catch (IOException ex) {
// report an IO exception
} catch (Exception ex) {
// report a general exception
}

Cover EXCEPTION in the catch block while using mockito

Function to be tested:
public void deleteFile(File file) {
try {
Files.delete(file.toPath()) ;
log.info("Csv file deleted from system succesfully after processing the data");
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Test code:
#Test
void testExceptionDuringDeletingFile() {
helper=mock(Helper.class);
//String bucket ="bucket";
//String key = "key";
File file = new File("");
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class,()->helper.deleteFile(file));
}
But I am getting the following exception:
org.mockito.exceptions.base.MockitoException: Checked exception is
invalid for this method! Invalid: java.io.IOException at
com.lululemon.product.ap.reprocess.producer.HelperTests.testExceptionDuringDeletingFile(HelperTests.java:191)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
Because your deleteFile() method doesn't throw the IOException. You catch it in your method implementation and do something with it.
And, your mocked Helper class instance really can't throw IOException from the deleteFile() method because the real deleteFile() method doesn't.
Throw the IOException from your method and you won't get the error.
deleteFile method:
public void deleteFile(File file) throws IOException {
try {
Files.delete(file.toPath());
log.info("Csv file deleted from system successfully after processing the data");
} catch (IOException e) {
throw new IOException("Couldn't delete file. Try again!");
}
}
Test method:
#Test
void testExceptionDuringDeletingFile() throws IOException {
File file = new File("");
Helper helper = mock(Helper.class);
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class, () -> helper.deleteFile(file));
}

How to use Suppliers.memoize when method throws Checked-Exception

I'm trying to use Suppliers#memorize on a function that throws IOException
Snippet:
private Supplier<Map> m_config = Suppliers.memoize(this:toConfiguration);
This gives an exception:
Unhandled exception type IOException
so I had to do something like this:
public ClassConstructor() throws IOException
{
m_config = Suppliers.memoize(() -> {
try
{
return toConfiguration(getInputFileName()));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
});
if(m_Configuration == null) {
throw new IOException("Failed to handle configuration");
}
}
I would like the CTOR to forward the IOException to the caller.
The proposed solution is not so clean, is there a better way to handle this situation?
Use UncheckedIOException
You're tagging java-8, so you should use the UncheckedIOException which is present for this very use case.
/**
* #throws java.io.UncheckedIOException if an IOException occurred.
*/
Configuration toConfiguration(String fileName) {
try {
// read configuration
} catch (IOException e) {
throw new java.io.UncheckedIOException(e);
}
}
Then, you can write:
m_config = Suppliers.memoize(() -> toConfiguration(getInputFileName()));

HtmlUnit throws ambiguous String error in Processing

I am trying to write code in Processing (basically Java) that will get the information from the table here: http://science.nasa.gov/iSat/iSAT-text-only/
I have not started writing the code specific to the table yet because I can't even get this to run. I get the error "type String is ambiguous." I cannot find any duplicate of String nor can I think of any other reason for this error.
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.*;
import java.net.*;
WebClient client = new WebClient(BrowserVersion.CHROME);
try {
java.lang.String url = "http://science.nasa.gov/iSat/iSAT-text-only/";
Page page = client.getPage(url);
println(page.getWebResponse().getContentAsString());
}
catch(IOException e) {
println("oops!");
}
UPDATE:
I modified the code slightly and got it to get the information I want using the following code in eclipse:
WebClient client = new WebClient();
String url = "http://science.nasa.gov/iSat/iSAT-text-only/";
HtmlPage page = null;
try {
page = (HtmlPage) client.getPage(url);
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client.waitForBackgroundJavaScript(10000);
System.out.print(page.asXml());
but it still throws the ambiguous String error in Processing.
Bizarrely, manually importing java.lang.String fixed the error.

Catch handler for multiple exceptions?

I am experimenting with exceptions and i want to ask when it is possible to handle multiple exceptions in one handler and when it is not?
For example i wrote the following code which combines two exceptions (FileNotFoundException OutOfMemoryError) and the program runs properly without any error. Al thought the handling is not so relevant with the functionality of the code i chose them just to see when i can combine multiple exceptions in on handler :
import java.io.FileNotFoundException;
import java.lang.OutOfMemoryError;
public class exceptionTest {
public static void main(String[] args) throws Exception {
int help = 5;
try {
foo(help);
} catch (FileNotFoundException | OutOfMemoryError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean foo(int var) throws Exception {
if (var > 6)
throw new Exception("You variable bigger than 6");
else
return true;
}
}
But when i choose different type of exceptions the compiler gives me error . For example when i choose IOException and Exception i have the error the exception is already handled " :
import java.io.IOException;
import java.lang.Exception;
public class exceptionTest {
public static void main(String[] args) throws Exception {
int help = 5;
try {
foo(help);
} catch (IOException | Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean foo(int var) throws Exception {
if (var > 6)
throw new Exception("You variable bigger than 6");
else
return true;
}
}
So why is this happening ? Why in one occasion i can use multiple exception in handler and in the other not ? Thank you in advance.
You are getting the message because IOException is a subclass of Exception. Therefore, if an IOException were thrown, it would be caught by a catch (Exception e) statement, so catching it as an IOException is redundant.
The first example works because neither FileNotFoundException nor OutOfMemoryError is a subclass the other.
However, you can catch sub-classed exceptions using the separate catch statement:
try{
// code that might throw IOException or another Exception
} catch (IOException e) {
// code here will execute if an IOException is thrown
} catch (Exception e) {
// code here will execute with an Exception that is not an IOException
}
If you do this, please note that the subclass must come first.

Categories

Resources