try-catch

Is it better to use try/catch instead of multiple IF statements?

自闭症网瘾萝莉.ら 提交于 2019-12-09 14:31:03
问题 Is it better, less expensive or more readable to use a try/catch block in Java instead of using multiple If statements to check user input for example? Example when parsing a Date string, won't it be better to parse directly using a try/catch block instead of writing multiple statements looking for illegal characters. In another example, say i want to read a file or stream, instead of using Scanner , I just force the method and wait for an exception to occur. Is that a healthy method of

Benchmark of Java Try/Catch Block

笑着哭i 提交于 2019-12-09 10:30:46
问题 I know that going into a catch block has some significance cost when executing a program, however, I was wondering if entering a try{} block also had any impact so I started looking for an answer in google with many opinions, but no benchmarking at all. Some answers I found were: Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum? Try Catch Performance Java Java try catch blocks However they didn't answer my question with facts, so I decided to

Putting try catch finally block inside another finally block

半世苍凉 提交于 2019-12-09 07:48:32
问题 try { } catch() {} finally { try { } catch() { } finally { } } Is it good to have the code like above? 回答1: Yes, you can do this. Actually, you are even required to do it when dealing with streams you want to close properly: InputStream in = /* ... */; try { } catch (...) { } finally { try { in.close(); } catch (...) { } finally { } } I don't see any case in which this would be a bad practice 回答2: For readability you can factor out the nested try-catch in to a separate method, like: try{

Why do try..catch blocks require braces?

六月ゝ 毕业季﹏ 提交于 2019-12-09 07:23:29
问题 While in other statements like if ... else you can avoid braces if there is only one instruction in a block, you cannot do that with try ... catch blocks: the compiler doesn't buy it. For instance: try do_something_risky(); catch (...) std::cerr << "Blast!" << std::endl; With the code above, g++ simply says it expects a '{' before do_something_risky(). Why this difference of behavior between try ... catch and, say, if ... else ? Thanks! 回答1: Straight from the C++ spec: try-block: try compound

Try-With Resource when AutoCloseable is null

对着背影说爱祢 提交于 2019-12-09 07:23:28
问题 How does the try-with feature work for AutoCloseable variables that have been declared null ? I assumed this would lead to a null pointer exception when it attempts to invoke close on the variable, but it runs no problem: try (BufferedReader br = null){ System.out.println("Test"); } catch (IOException e){ e.printStackTrace(); } 回答1: The Java Language Specification specifies that it is closed only if non-null, in section 14.20.3. try-with-resources: A resource is closed only if it initialized

How does the try catch finally block work?

若如初见. 提交于 2019-12-09 05:04:19
问题 In C# , how does a try catch finally block work? So if there is an exception, I know that it will jump to the catch block and then jump to the finally block. But what if there is no error, the catch block wont get run, but does the finally block get run then? 回答1: Yes, the finally block gets run whether there is an exception or not. Try [ tryStatements ] [ Exit Try ] [ Catch [ exception [ As type ] ] [ When expression ] [ catchStatements ] [ Exit Try ] ] [ Catch ... ] [ Finally [

Python try except else invalid syntax?

别来无恙 提交于 2019-12-09 03:53:04
问题 So I am trying to set up a small script in Python's IDLE. The IDLE syntax check tells me this code has a syntax error: from ftplib import FTP import os def ftpconnect(address, username, password): ftp_connection = 0 ftp = FTP(address) try: ftp.login(username, password) print(ftp.getwelcome()) if ftp.getwelcome() == '220 FTP Connected!': return 1 else: return 0 print(ftpconnect('10.10.10.xxx', 'xxx', 'xxx')) The syntax error comes anywhere that I try to get out of the "try" statement, here

Throw a format exception C#

馋奶兔 提交于 2019-12-09 03:38:14
问题 I'm trying to throw a format exception in the instance someone tries to enter a non-integer character when prompted for their age. Console.WriteLine("Your age:"); age = Int32.Parse(Console.ReadLine()); I'm unfamiliar with C# language and could use help in writing a try catch block for this instance. Thanks very much. 回答1: That code will already throw an FormatException . If you mean you want to catch it, you could write: Console.WriteLine("Your age:"); string line = Console.ReadLine(); try {

requests.HTTPError uncaught after a requests.get() 404 response

拥有回忆 提交于 2019-12-09 01:38:02
问题 I'm having a slight problem with the requests library. Say for example I have a statement like this in Python: try: request = requests.get('google.com/admin') #Should return 404 except requests.HTTPError, e: print 'HTTP ERROR %s occured' % e.code For some reason the exception is not being caught. I've checked the API documentation for requests but it's a bit slim. Is there anyone who has more experience with the library that might be able to help me out? 回答1: Interpreter is your friend:

Why does Try-Catch require curly braces

痞子三分冷 提交于 2019-12-08 22:22:12
问题 Just curious: Why is the syntax for try catch in C# (Java also?) hard coded for multiple statements? Why doesn't the language allow: int i; string s = DateTime.Now.Seconds % 2 == 1 ? "1" : "not 1"; try i = int.Parse(s); catch i = 0; The example is for trivial purposes only. I know there's int.TryParse. 回答1: Consider the fact that there are really three (or more) code blocks in play here: try {} catch (myexcption) {} catch (myotherexception) {} finally {} Keep in mind that these are in the