try-catch

Infinite While Loop When InputMidmatchException is caught in try-catch block [duplicate]

不羁岁月 提交于 2019-12-29 08:55:35
问题 This question already has answers here : try/catch with InputMismatchException creates infinite loop (7 answers) Closed 4 years ago . I keep getting my code caught in an infinite while loop. It is nothing to advanced, but i can not figure it out for the life of me! Someone Please help I have purplosely just re created the specific error without all of the if statements i have in my actual program. package bs; import java.util.InputMismatchException; import java.util.Scanner; public class bs {

C++: Throwing a derived class by reference does not work when catching base class

痞子三分冷 提交于 2019-12-29 06:46:06
问题 I want to throw my own exceptions with the base class Exception . There is a virtual method print which will be overwritten by the subclasses. I only catch the type Exception& and use print to get the specific error. The problem is that once I throw a reference of a subclass it is trated as if it were the base class. Here is an example: #include <iostream> using namespace std; class Exception { public: virtual void print() { cout << "Exception" << endl; } }; class IllegalArgumentException :

What is faster: try catch vs Promise

99封情书 提交于 2019-12-29 05:30:23
问题 I heard such an opinion that you should avoid usage of try/catch at all because it takes many resources. So could promise error handling could be faster? Or it does not matter at all? function f(somethingDangerous) { return new Promise((resolve, reject) => { // try { // somethingDangerous(); // resolve(); // } catch (err) { // reject(err); // } // VS somethingDangerous(); resolve(); }).catch((err) => { console.error('Catched: ' + err); }); } f(() => {throw 'DANGEROUS THING';}); UPD : I know

using catch(…) (ellipsis) for post-mortem analysis

江枫思渺然 提交于 2019-12-29 05:13:06
问题 Someone in a different question suggested using catch(...) to capture all otherwise unhandled - unexpected/unforseen exceptions by surrounding the whole main() with the try{}catch(...){} block. It sounds like an interesting idea that could save a lot of time debugging the program and leave at least a hint of what happened. The essence of the question is what information can be recovered that way (other than whatever debug globals I leave behind), and how to recover it (how to access and

How To Check If A Method Exists At Runtime In Java?

拈花ヽ惹草 提交于 2019-12-28 13:46:51
问题 How would one go about checking to see if a method exists for a class in Java? Would a try {...} catch {...} statement be good practice? 回答1: I assume that you want to check the method doSomething(String, Object) . You might try this: boolean methodExists = false; try { obj.doSomething("", null); methodExists = true; } catch (NoSuchMethodError e) { // ignore } This will not work, since the method will be resolved at compile-time. You really need to use reflection for it. And if you have

Try / Catch in Constructor - Recommended Practice?

笑着哭i 提交于 2019-12-28 12:01:43
问题 Something I've always been curious of public class FileDataValidator { private String[] lineData; public FileDataValidator(String[] lineData){ this.lineData = lineData; removeLeadingAndTrailingQuotes(); try { validateName(); validateAge(); validateTown(); } catch(InvalidFormatException e) { e.printStackTrace(); } } //validation methods below all throwing InvalidFormatException Is is not advisable to include the try/catch block within my Constructor? I know I could have the Constructor throw

Try / Catch in Constructor - Recommended Practice?

一笑奈何 提交于 2019-12-28 12:01:14
问题 Something I've always been curious of public class FileDataValidator { private String[] lineData; public FileDataValidator(String[] lineData){ this.lineData = lineData; removeLeadingAndTrailingQuotes(); try { validateName(); validateAge(); validateTown(); } catch(InvalidFormatException e) { e.printStackTrace(); } } //validation methods below all throwing InvalidFormatException Is is not advisable to include the try/catch block within my Constructor? I know I could have the Constructor throw

Is there a preference for nested try/catch blocks?

戏子无情 提交于 2019-12-28 04:50:33
问题 One of the things that always bugs me about using Readers and Streams in Java is that the close() method can throw an exception. Since it's a good idea to put the close method in a finally block, that necessitates a bit of an awkward situation. I usually use this construction: FileReader fr = new FileReader("SomeFile.txt"); try { try { fr.read(); } finally { fr.close(); } } catch(Exception e) { // Do exception handling } But I've also seen this construction: FileReader fr = new FileReader(

C++ try/throw/catch => machine code

放肆的年华 提交于 2019-12-28 03:24:50
问题 Mentally, I've always wondered how try/throw/catch looks behind the scenes, when the C++ compiles translates it to assembler. But since I never use it, I never got around to checking it out (some people would say lazy). Is the normal stack used for keeping track of try s, or is a separate per-thread stack kept for this purpose alone? Is the implementation between MSVC and g++ big or small? Please show me some pseudo asm (IA-32 is ok too) so I never have to check it out myself! :) Edit: Now I

try..catch not catching async/await errors

大城市里の小女人 提交于 2019-12-28 01:28:31
问题 Perhaps I misunderstood how catching errors with async/await is supposed to work from things articles like this https://jakearchibald.com/2014/es7-async-functions/ and this http://pouchdb.com/2015/03/05/taming-the-async-beast-with-es7.html, but my catch block is not catching 400/500. async () => { let response try { let response = await fetch('not-a-real-url') } catch (err) { // not jumping in here. console.log(err) } }() example on codepen if it helps 回答1: 400/500 is not an error, it's a