Ads
好啦既然大家咁熱心, 我又要問多幾個問題, 因為我又唔識 :^( :^( :^(
throw Exception定係Return Null/Error number 好D?
以java為例
Throw exception真係好煩,
1)有時唔知點分係邊個case
2)淨係見到個try catch block都唔開胃
3)話就話Exception可以上多幾層 stack, 咪又係每個method都係加返throws declaration
Try catch 好好用
Debug 都易啲因爲你會知到邊到出錯
你可以加自己message 同 run time value
同埋會 bubble up
覺得有機會有 exception 就 handle 咗
加埋 finally 去 gracefully terminate 其他嘢
點解會唔開胃 :^( :^(
點樣加runtime value :^( :^( :^( :^( :^(
你唔覺try-catch-finally好佔版面之餘, 仲要D logic飛黎飛去好混亂 :^(
bubble up 又係咩意思 :^( :^(
不如我舉個例子 :^( :^(
假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement
public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}
Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}
public Foo parse1(String input) throws Exception {
Foo ret = new Foo();
// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();
return ret;
}
public Foo parse2(String input) {
Foo ret = new Foo();
// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;
return ret;
}
你 catch 完可以 create a new exception with custom message, with run-time value and re throw
咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up
try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose
利申講緊 C#. Java etc 可能有別
學左兩年都仲好似半桶水
DESIGN PATTERN學左
但好多技巧都唔識,
E.G.今日睇到Convention over Configuration又發現自己一路做得唔好 :^( :^(
E.G.落手做做極都唔可以一勞永逸
等等
學到好LOST
Design Pattern係對特定情景既model answer,
但你唔識中間既推導過程, 你遇上open question既時候就會gg
基本上講Design Pattern既書都一定會先講Design Principle
Design Principle才是判斷是否好code既基本工
btw, 個人推薦不寫if學習法
一直堅持唔寫if, 直到必要時才用
何必驕枉過正, 我一直都係話三思先用if, if對底層係好重要
但好多時咩為之靚code唔係我扔條絕對rule出黎就可以寫到
寫code前三思好重要
你自己既靚program定義係咩先
我就覺得考慮到time complexity, maintenance同readability就已經好夠
第一樣 我覺得真係要望多d 唔同algorithm
第二樣 要你長期做同一個project你先會研究到最好既solution
第三樣 要有teammate插你d code佢睇唔明
所以,都係出去搵份工啦 :^(
不如我舉個例子 :^( :^(
假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement
public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}
Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}
public Foo parse1(String input) throws Exception {
Foo ret = new Foo();
// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();
return ret;
}
public Foo parse2(String input) {
Foo ret = new Foo();
// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;
return ret;
}
其實好多時候都會extend個新嘅exception class出嚟
學左兩年都仲好似半桶水
DESIGN PATTERN學左
但好多技巧都唔識,
E.G.今日睇到Convention over Configuration又發現自己一路做得唔好 :^( :^(
E.G.落手做做極都唔可以一勞永逸
等等
學到好LOST
Design Pattern係對特定情景既model answer,
但你唔識中間既推導過程, 你遇上open question既時候就會gg
基本上講Design Pattern既書都一定會先講Design Principle
Design Principle才是判斷是否好code既基本工
btw, 個人推薦不寫if學習法
一直堅持唔寫if, 直到必要時才用
未聽過不寫if 學習法
但我認同係會有用
除非用java/C,我覺得不寫loop 學習法都會work
Ads
想寫得靚,首先Design pattern 最好睇一次
唔洗刻意用. 因為好多時候每個pattern 都係某個情況出現先會用
例如你寫if else, 發覺不斷重覆,你就可以用design pattern 其中幾招 (factory pattern ) 去remove o左d if else.
又例如你發現想用single object 先用singleton.
每個pattern 都有佢既用處,但又會多o左d限制。
另外最好睇下d arch code
例如MVC MVP MVVM.
呢兩年最興係observer pattern 都好值得睇
例如rx .
多d睇github, 睇下勁人點寫,唔好自己用死某個pattern .
convention over cofigutaion 呢個係ruby on rails 核心既野,有興趣search ruby girl 用三十分鐘玩下有幾神奇 :^(
你 catch 完可以 create a new exception with custom message, with run-time value and re throw
咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up
try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose
利申講緊 C#. Java etc 可能有別
我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝 :^(
你 catch 完可以 create a new exception with custom message, with run-time value and re throw
咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up
try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose
利申講緊 C#. Java etc 可能有別
我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝 :^(
唔知你是咪真心 :^(
但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java
https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
你 catch 完可以 create a new exception with custom message, with run-time value and re throw
咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up
try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose
利申講緊 C#. Java etc 可能有別
我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝 :^(
唔知你是咪真心 :^(
但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java
https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
佢講既message parameter係比你LOG低
你諗住用黎判斷情形? :^( :^(
你 catch 完可以 create a new exception with custom message, with run-time value and re throw
咁call 有 exception 個 function 就會見到你 throw 嘅 custom message. (Optional).
咁一層一層上好似叫 bubble up
try catch finally 其實好有用因為你開咗 FB connection or file handle etc 可以用 finally 閂返
不過呢樣用 using 都做到. 有 exception 時佢會call Dispose
利申講緊 C#. Java etc 可能有別
我C#同JAVA都寫過
C#好似有個MAP可以比你攝野
但JAVA我真係唔知點攝 :^(
唔知你是咪真心 :^(
但 the constructor of exception and its subclasses accepts a message parameter. You can include the caught exception as part of the bubble up as well. It is used quite a bit in c#, and I just checked that it is similar in java
https://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html
佢講既message parameter係比你LOG低
你諗住用黎判斷情形? :^( :^(
C#個Exception有個prop叫Data
果個K/V PAIR就真係比你攝有關EXCEPTION既資料
唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.
可以放 string 喎 :^(
點解講到咁複雜嘅 :^( :^(
唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.
可以放 string 喎 :^(
點解講到咁複雜嘅 :^( :^(
個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好
唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.
可以放 string 喎 :^(
點解講到咁複雜嘅 :^( :^(
個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好
我一直都係話寫個error message
你可以 throw new exception("error in parameter" + validNumber + " expected but " + actualValue + " found.", ex)
你都可以放多啲你需要用啲 debug 嘅資料入去
完全唔使 parse 乜嘢
本身c# doc 都有用呢個 logging 做 example.
唔明你所指嘅 parsing 係指乜?
Sorry for using string concat and my possible typos. Using my phone right now
唔熟 java. C# 你可以放 expected value > 5; actual = -3 之類入去 個 message.
可以放 string 喎 :^(
點解講到咁複雜嘅 :^( :^(
個message應該係expect你放human readable既文字,用黎print或者log低
而唔係比你格硬PARSE
如果你要傳exception state,咁既方法我認為唔多好
我一直都係話寫個error message
你可以 throw new exception("error in parameter" + validNumber + " expected but " + actualValue + " found.", ex)
你都可以放多啲你需要用啲 debug 嘅資料入去
完全唔使 parse 乜嘢
本身c# doc 都有用呢個 logging 做 example.
唔明你所指嘅 parsing 係指乜?
Sorry for using string concat and my possible typos. Using my phone right now
:^( :^( :^(
我講緊既係,你要比program了解你既ERROR係乜
從而係program logic層面進行唔同既應對方案
Exception can bubble up with rethrow so the calling functions know shit happened
If then can't achieve that
Ads
想寫得靚,首先Design pattern 最好睇一次
唔洗刻意用. 因為好多時候每個pattern 都係某個情況出現先會用
例如你寫if else, 發覺不斷重覆,你就可以用design pattern 其中幾招 (factory pattern ) 去remove o左d if else.
又例如你發現想用single object 先用singleton.
每個pattern 都有佢既用處,但又會多o左d限制。
另外最好睇下d arch code
例如MVC MVP MVVM.
呢兩年最興係observer pattern 都好值得睇
例如rx .
多d睇github, 睇下勁人點寫,唔好自己用死某個pattern .
convention over cofigutaion 呢個係ruby on rails 核心既野,有興趣search ruby girl 用三十分鐘玩下有幾神奇 :^(
聽講最近少咗人用 ruby, 但見學過嘅朋友讚口不絕
好似話自從 twitter 唔用佢就少咗好多人用
請問
有冇用過嘅巴絲講解下 RoR 嘅利弊 :^( :^(
假設我有一個parse function, 係 public Foo parse(String input);
咁當個input係古怪野, 我應該return null定throw ex?
我會prefer foo2做法, 因為foo1我要格硬拆個declaration出黎, 又要專登係個method declaration加throws statement
public void MyCaller() {
Foo foo1 = null;
try {
foo1 = parse1(this.str);
} catch (Exception ex) {
// handle, log error and return
}
Foo foo2 = parse2(this.str);
if (foo2 == null) {
// handle, log error and return
}
}
public Foo parse1(String input) throws Exception {
Foo ret = new Foo();
// do my parsing stuff...
int f1 = getF1(input);
if (f1 < 0) throw new Exception();
return ret;
}
public Foo parse2(String input) {
Foo ret = new Foo();
// do my stuff...
int f1 = getF1(input);
if (f1 < 0) return null;
return ret;
}