exception-handling

Why is it better to throw an exception rather than return an error code?

旧时模样 提交于 2019-12-20 08:49:09
问题 Legacy error handling tends to follow the method that all functions return a code depending on success/failure. You would check this code and handle (if an error) appropriately. Modern programming languages however follow the exception model, where if something exceptional happens that cannot be handled properly an exception gets thrown - which keeps bubbling up until its handled. My question is why have we moved towards the exception model ? What are the reasons behind this? Why is it better

Aggregate runtime exceptions in Java 8 streams

给你一囗甜甜゛ 提交于 2019-12-20 08:35:57
问题 Let's say I have a method which throws a runtime exception. I'm using a Stream to call this method on items in a list. class ABC { public void doStuff(MyObject myObj) { if (...) { throw new IllegalStateException("Fire! Fear! Foes! Awake!"); } // do stuff... } public void doStuffOnList(List<MyObject> myObjs) { try { myObjs.stream().forEach(ABC:doStuff); } catch(AggregateRuntimeException??? are) { ... } } } Now I want all items in the list to be processed, and any runtime exceptions on

Does 'finally' always execute in Python?

痴心易碎 提交于 2019-12-20 08:00:11
问题 For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed? For example, let’s say I return while in an except block: try: 1/0 except ZeroDivisionError: return finally: print("Does this code run?") Or maybe I re-raise an Exception : try: 1/0 except ZeroDivisionError: raise finally: print("What about this code?") Testing shows that finally does get executed for the above examples, but I imagine there are other scenarios I haven't thought of.

Why I m getting Unreported Exception

纵饮孤独 提交于 2019-12-20 07:58:10
问题 can you please why error comes in line 13 as unreported exception ,must be caught pr declared to be thrown class Demo { public static void main(String args[]) { try { int x = 43 / 0; } catch (ArithmeticException ob) { throw ob; } try { int x = 43 / 0; } catch (Exception ob) { throw ob; } Exception ob = new Exception(); throw ob; // Line 13 unreported exception Exception; must be caught or declared to be thrown } } 回答1: You need to add a throws to the method that throws exceptions as mentioned

How does one decide to create a checked excpetion or an unchecked exception [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 07:25:42
问题 This question already has answers here : Understanding checked vs unchecked exceptions in Java (21 answers) Closed 3 years ago . I want to know how does one know to create and throw a checked exception or an unchecked exception. For example I have a service which takes some data and validates it before using it. During validation a certain field did not meet the rules and I want to throw an exception say ValidationException(). How will I know of decide it should be checked or unchecked. In

How does one decide to create a checked excpetion or an unchecked exception [duplicate]

≯℡__Kan透↙ 提交于 2019-12-20 07:25:03
问题 This question already has answers here : Understanding checked vs unchecked exceptions in Java (21 answers) Closed 3 years ago . I want to know how does one know to create and throw a checked exception or an unchecked exception. For example I have a service which takes some data and validates it before using it. During validation a certain field did not meet the rules and I want to throw an exception say ValidationException(). How will I know of decide it should be checked or unchecked. In

exception handling in the implemented method

天涯浪子 提交于 2019-12-20 06:29:04
问题 The code below gives a checked error to throws Exception : import java.io.IOException; interface some { void ss99() throws IOException; } public class SQL2 implements some { @Override public void ss99 () throws Exception {} // ... } while the one below compiles fine: import java.io.IOException; interface some { void ss99() throws IOException; } public class SQL2 implements some { @Override public void ss99 () throws NullPointerException {} // ... } On what logic is Java doing this-- any ideas

Separate exception handler for PDO

一世执手 提交于 2019-12-20 06:25:17
问题 In my PHP code, I have several types of exceptions. One is a 'normal' Exception , another is PDOException . I'm using set_exception_handler($handler) to catch exceptions automatically. Is there any way I can get separate handlers for Exception and PDOException? If not, can I check the type of the exception in the handler? 回答1: I think you should have a "global" handler, and branch. set_exception_handler(function ($exception) { if ($exception instanceof PDOException) { handle_pdo_exception(

unexpected execution of Exceptions in python

拥有回忆 提交于 2019-12-20 05:52:21
问题 I am novice to python and i came up with this problem. i have made a simple program for calculator. In add function, i have used try- except. when this line is encountered (if decide== 'no' or decide== 'n':), it displays the print line "return(" You have exited ")" but it also throws the exception. I cant understand why. import sys def menu(): print "calculator using functions" print "Choose your option:" print " " print "1) Addition" print "2) Subtraction" print "3) Multiplication" print "4)

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0

老子叫甜甜 提交于 2019-12-20 05:52:15
问题 I'm new at learning java, and while following a tutorial from a book, I received this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0. I've tried researching from the net to find out more about the error, and I could not find the answer to this. To make things worse, the book's website no longer exists when I tried to go to their website. The program is about calculating payments for 2 types of workers, Engineers and Technicians including Over time pays which is 1