Error Handling
Code:
public class Main {
public static void main(String[] args) {
//declarations
//String s;...need to assign value
String s = null;
System.out.println(s);
//Arrays
String[] strings = {"Welcome!"}; //one item in array
System.out.println(strings[1]); // error, no [1], only [0]
//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
//at Main.main(Main.java:13)
}
}
Exception Handling
Code:
public class Main {
public static void main(String[] args) {
//try catch block
try {
String[] strings = {"Welcome!"};
System.out.println(strings[1]);
} catch (ArrayIndexOutOfBoundsException e) {
//e.printStackTrace();
System.out.println("Error occurred");
}
// without try/catch block
// Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
// at Main.main(Main.java:6)
//with try/catch blcok
//The application is still running!
//java.lang.ArrayIndexOutOfBoundsException: 1
//at Main.main(Main.java:9)
//with error handling
//Error occurred
//The application is still running!
System.out.println("The application is still running!");
}
}
Extracting and Error Handling with Try/Catch Block: Revisited
String[] strings = {"Welcome!"};
System.out.println(strings[1]);
..
getArrayItem(); //refactored getArrayItem()
..
private static void getArrayItem()
throws ArrayIndexOutOfBoundsException {
String[] strings = {"Welcome!"};
System.out.println(strings[1]);
}
try {
getArrayItem(); //refactored getArrayItem()
} catch (ArrayIndexOutOfBoundsException e) {
// e.printStackTrace(); // <--- throws ugly message
System.out.println("Array item was out of bounds");
}
Code:
public class Main {
public static void main(String[] args) {
//Extrac Method with Error Handling
//Surround with try/catch blcok
try {
getArrayItem(); //refactored getArrayItem()
} catch (ArrayIndexOutOfBoundsException e) {
// e.printStackTrace(); // <--- throws ugly message
System.out.println("Array item was out of bounds");
//Array item was out of bounds
}
}
private static void getArrayItem()
throws ArrayIndexOutOfBoundsException {
String[] strings = {"Welcome!"};
System.out.println(strings[1]);
}
}
Debugger
Finding Possible Exceptions
Highlight command > Help > Dynamic Help > JavaDoc > CONSTRUCTOR > METHOD
URI uri = new URI("http:\\somecompany.com");
java.net.URISyntaxException: Illegal character in opaque part at index 5: http:\somecompany.com
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at Main.main(Main.java:10)
From e.printStackTrace();
To System.out.println(e.getMessage());
Code:
import java.net.URI;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) {
//Uniform Resource Identifier
try {
URI uri = new URI("http:\\somecompany.com");
} catch (URISyntaxException e) {
System.out.println(e.getMessage());
/*
e.printStackTrace();
java.net.URISyntaxException: Illegal character in opaque part at index 5: http:\somecompany.com
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at Main.main(Main.java:10)
*/
}
System.out.println("I'm alive!");
//Exception in thread "main" java.lang.Error: Unresolved compilation problem:
// Unhandled exception type URISyntaxException
// at Main.main(Main.java:8)
}
}
- Log in to post comments