Skip to content

Instantly share code, notes, and snippets.

View BalicantaYao's full-sized avatar

Balicanta BalicantaYao

View GitHub Profile
public void demoException() throws Exception{
try{
throw new ("Exception A");
} catch (Exception e){
throw new Exception("Exception B");
} finally {
throw new Exception("Exception C");
}
}
public static class verfiyExceptionTest{
@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void throwsExceptionWithMessage() {
// 預期 LoginFailed 會產生以及對應的 Exception message
thrown.expect(LoginFailedException.class);
thrown.expectMessage(startsWith("UserName"));
public void checkEmptyUserName(String username){
if(StringUtils.isBlank(userName)){
throws new LoginFailedException("Username is empty!");
}
}
public static void main (String args[]){
ServerSocket serverSocket = new ServerSocket(199);
try{
Socket socket = serverSocket.accept();
}
finally{
socket.close();
}
}
private static void runCase2() {
int j = 10;
Point point = new Point(4, 4);
logger.info(“Before Modify Point {}, j ={}”, point, j);
setPointWithAssign(point, j);
logger.info(“After Modify Point {}, j ={}”, point, j);
// Result
// Before Modify Point Point{y=4, x=4}, j =10
private static void runCase1() {
int j = 10;
Point point = new Point(4, 4);
logger.info(“Before Modify Point {}, j ={}”, point, j);
setPointLocation(point, j);
logger.info(“After Modify Point {}, j ={}”, point, j);
// Result
// Before Modify Point Point{y=4, x=4}, j =10
class Base{
public void foo() throws FileNotFoundException {
thorw new FileNotFoundException();
}
}
class OverideTest extends Base{
public void foo() throws IOException{
throw new IOException();
}
try{
Lazy sample = new Lazy();
l.foo(1);
} catch (Exception1 exec){
// Handle
// 在這裡你很難發現 Exception 是 Exception1 or Exception 2 or Exception3
}
class Exception1 extends Exception{}
class Exception2 extends Exception1{}
class Exception3 extends Exception2{}
class Lazy{
public void foot(int i) throws Exception1 {
if(i == 1){
throw new Exception1();
} else if(i==2){
throw new Exception2();
} else if(i==3){
@BalicantaYao
BalicantaYao / FileReadExample.java
Created May 5, 2017 14:14
讀取檔案內容範例
public class FolderRead {
public static void main(String[] args) throws IOException {
// files 路徑
File files1 = new File("files");
File[] files2 = files1.listFiles();
for (File file : files2) {