This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void demoException() throws Exception{ | |
try{ | |
throw new ("Exception A"); | |
} catch (Exception e){ | |
throw new Exception("Exception B"); | |
} finally { | |
throw new Exception("Exception C"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void checkEmptyUserName(String username){ | |
if(StringUtils.isBlank(userName)){ | |
throws new LoginFailedException("Username is empty!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main (String args[]){ | |
ServerSocket serverSocket = new ServerSocket(199); | |
try{ | |
Socket socket = serverSocket.accept(); | |
} | |
finally{ | |
socket.close(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base{ | |
public void foo() throws FileNotFoundException { | |
thorw new FileNotFoundException(); | |
} | |
} | |
class OverideTest extends Base{ | |
public void foo() throws IOException{ | |
throw new IOException(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
Lazy sample = new Lazy(); | |
l.foo(1); | |
} catch (Exception1 exec){ | |
// Handle | |
// 在這裡你很難發現 Exception 是 Exception1 or Exception 2 or Exception3 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |