Most voted "exception" questions
The exception treatment is the mechanism responsible for the processing of occurrence of conditions that change the normal flow of code execution.
Learn more…329 questions
Sort by count of
-
100
votes4
answers4254
viewsWhy should we avoid returning error codes?
It is common to say that one should avoid returning error codes in a function when some operation fails. It is not hard to see that error codes are confusing (is it a valid value or an error?) and…
-
81
votes5
answers3667
viewsIs there any downside to always capturing Exception and not something more specific?
When I write code in Java and try to treat exceptions I usually simply use the superclass Exception, not linking to the specific class and it usually works. There is some inconvenience in doing…
-
74
votes5
answers2194
viewsExceptions consume a lot of processing. Truth or legend?
I’ve seen shows like this : if(!clienteExiste(1)) { return "Cliente não existe."; } and others like that if(!clienteExiste(1)) { throw new Exception("Cliente não existe."); } The second code will…
-
59
votes6
answers10591
views -
55
votes4
answers9003
viewsBest way to deal with Exceptions
During my work I learned a way to deal with Exceptions, but I don’t know if it’s very good. Here’s an example of the code: class Program { private static void Main(string[] args) { try { Foo(); }…
-
46
votes2
answers11298
viewsHow does Try-with-Resources work?
In Java 7, the concept of Try-with-Resources was added in the language. What is the Try-with-Resources? How does it work? What does it serve? How is it used? What problem does it aim to solve?…
-
35
votes2
answers3305
viewsHow to best treat exceptions in Java?
Who has created great programs in Java knows of a very common headache: Do not know where to play the flow of Exceptions. Many decide to do things like: Treat everything as Exception (the superclass…
-
34
votes4
answers2538
viewsShould exceptions be used for flow control?
When I studied Java, I learned that exceptions should only be used in situations, well, "exceptional" (i.e. an error, an unforeseen condition, etc), never for normal flow control. Not only because…
-
27
votes4
answers3196
viewsWhat is good practice when casting an exception within if?
What is the best practice in this case, use the Else even knowing that the if will or will not make an exception? option 1 (with Else): if (condicao){ throw new RuntimeException("Mensagem"); }else{…
-
27
votes6
answers8712
viewsHow and when to use Finally after a Try?
First I would like to know why in using the finally What are its advantages? It really only serves for when an exit occurs from within the scope of a block try-catch, as when some return or goto…
-
27
votes5
answers31170
viewsWhat are Try/Catch Blocks for and when should they be used?
I’ve been looking for some explanations on the subject, but I couldn’t find one that was simple and straightforward. My question is : What Try/Catch Blocks are for and when they should be used?…
-
26
votes1
answer1329
viewsIs it good practice to make an exception in these cases?
I’m solving some object orientation exercises. One of the exercises asks to write a class that represents an airplane flight, containing the date, number and methods to check the status of seats.…
-
23
votes3
answers8791
viewsWhat is the difference between checked (checked) and unchecked (unchecked) exceptions?
Hello! I have some questions about exceptions, which are they: What is the main difference between checked and unchecked exceptions? In which situations should I use each of them? What are good…
-
22
votes4
answers33277
viewsUsing the keywords Throws and Throw
How do I use the words Throws and Throw in a Java code?
-
20
votes1
answer8743
viewsPython, difference between assert and raise?
I came across a question with the structures raise and assert python. In the code below shows the implementation of structures forcing an error if the number passed is negative. def…
-
19
votes3
answers736
viewsWhat kind of treatment can be performed in this case?
At the moment I want to learn more about exception treatment, a topic that is very well commented by @Maniero. I read several of his answers about exceptions, but I still had a question. The method…
-
18
votes1
answer2737
viewsWhat’s the difference between "throw" and "throw ex"?
I saw that the C# allows to do... try { //algum código } catch { throw; } also... try { //algum código } catch (Exception ex) { throw ex; } and... try { //algum código } catch (Exception) { throw; }…
-
17
votes2
answers4913
viewsHow to check if a file is in use without launching C#exception
I have an application that processes a file queue. I need to open the files for reading and writing. Sometimes the files are in use when I go to process them. How can I check if the file is in use?…
-
17
votes1
answer990
viewsIs using an empty catch a bad practice?
There have been cases where I needed to make use of methods that launched exception if any error occurred, but the exception would stop the execution of script, what would be something unwanted. For…
-
15
votes3
answers1122
viewsIs it possible to manipulate PHP errors via code?
As you all know, PHP returns runtime syntax errors in the browser, such as fatal errors and exceptions. It is possible to handle these errors via code so that I can save to the database in a table…
-
15
votes2
answers288
viewsWhere should exceptions be handled to display to the system user?
What is the best place to handle an exception and send a message to the system user in a desktop application? In the controller, in the view or elsewhere?
-
14
votes2
answers529
viewsWhat is checked in the code in C#?
I saw a code: using (IEnumerator<TSource> e = source.GetEnumerator()) { checked { while (e.MoveNext()) count++; } } What is the purpose of the code checked {} in that code C#?…
-
14
votes4
answers1204
viewsSpecific use of Try-catch
I am studying Hibernate and with it came some doubts that have come to me before but I did not question in depth. public void salvar(Usuario usuario){ Session sessao = null; Transaction transacao =…
-
13
votes3
answers514
viewsJava / Android: how to view Stacktrace log
How can I display the whole exception error code (Fullstacktrace) in Android Log? try { //CODE }catch (Exception e){ Log.e(TAG,e.getStackTrace()); }…
-
13
votes1
answer796
viewsHow to treat exception in a Webview?
I wanted to know how to treat exceptions in my Webview, because sometimes the site ta off, and the page does not load, and sometimes even crashes the application. I tried a try catch no load, but…
-
13
votes2
answers869
viewsWhat is Finally for in PHP 5.5?
PHP 5.5 implemented a feature (which I’ve heard exists in other languages) called finally, in the handling of exceptions (together with the try/catch) We have the following example:…
-
12
votes2
answers2347
viewsWhy does my Try/catch only accept "Exception and" (Generic)?
I want to specify the exceptions, not in this general way but the item.write only accepted Exception e public File saveFile(FileItem item, File dirFile, String filename) { dirFile.mkdirs(); File…
-
12
votes1
answer191
viewsIs it correct for a method to make the same exception for two different reasons?
I’m practicing TDD simulating an alarm system. Alarm centers work connected to sensors that detect intrusion (opening a door or window, or movement within a room, for example). They have a fixed…
-
11
votes3
answers505
viewsWhat are the native exceptions of PHP?
Where it is possible to check all exceptions (native) that can be released by PHP? I searched and only found ways to treat with try/catch.
-
11
votes2
answers435
viewsWhat are Exceptions and how should I create and embed them in PHP
I’m creating an MVC system and I want to implement Exceptions in my system. I’ve just never particularly used it. So I wanted to know how to apply the Exceptions, since I saw here in Stackoverflow…
-
10
votes2
answers882
viewsStandard for typifying errors/exceptions?
As far as I know, Javascript has no error typing. In C# and Java, it is possible to do things like: try { /* .. snip .. */ } catch (FooException foo) { /* .. snip .. */ } catch (BarException bar) {…
-
10
votes3
answers129
viewsHow to create mandatory exceptions in C#?
Is there any way to make a method make an exception that must be dealt with when the method is invoked? I used the throw new Exception("mensagem de erro") but the treatment is still optional when…
-
10
votes2
answers860
viewsWhat are the Exceptions?
A little while ago I asked that question What are Exceptions and how should I create and embed them in PHP, but I did not have an answer that satisfactorily addressed the points raised. So I am…
-
10
votes5
answers15150
viewsHow to avoid "Cannot read Property ... of Undefined" error?
Considering the following code: var a = {}; if (a.b.c !== undefined) { console.log("definido!"); } else { console.log("indefinido!"); } Is there any way to check each property without having to test…
-
10
votes2
answers97
viewsIs it correct to program returning exceptions to the expected event?
When we are developing a software we can go through situations where something happens that we are not expecting and usually when it happens in many programming languages we can treat it within…
-
9
votes1
answer216
viewsPointer variable is declared null, but member function performs normally
Below follows an example of the code I’m trying to execute. //main.cpp #include <iostream> using namespace std; class A{ public: A(){} ~A(){} void teste(){ cout << "Teste" << endl;…
-
9
votes1
answer3553
viewsHow to use a Try catch inside another?
I need to write data into a database, and if it succeeds, then I will generate a .pdf with PHP. Even if I put one try catch in a part of the code and it gives the exception, still the rest of the…
-
9
votes2
answers286
viewsIs it good practice to always manage mistakes with exceptions?
I’m creating a game with Python 3.4 and Pygame, and I’m using the paradigm object-oriented. In my classes, especially in __init__, am full the type checkings, to prevent Runtime errors. For example,…
-
9
votes5
answers819
viewsIs the rest of the code inside Try executed after finding Exception?
The rest of the existing code within a try is executed after finding a exception or jumps right into the code inside the catch(Exception e) try{ x = 10; //alguns erro de try catch x = 20; } catch…
-
9
votes2
answers331
viewsHow to implement a "slim" controller
I am working on a prototype of an ASP.NET MVC application, where I want to leave my controller lean (with the least amount of code possible), for such, not making business logic in it, but rather in…
-
9
votes4
answers785
viewsShould I use a "Try-catch" to identify if a password is wrong?
On the screen of login, carry out the verification in the bank by means of a select, and I’m using the catch to capture this exception. It is correct to use the catch for that guy? if (Usuario !=…
-
8
votes1
answer1071
viewsHow to launch my own exceptions with PDO?
When running the line below, in the constructor of my database access class: $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); PDO will report errors by releasing type…
-
8
votes3
answers2145
viewsHow to display the exception message(ex.getMessage()) launched by the server in a jquery ajax?
@GET @Path("boleto/{processo}/{resposta}") @Produces({ "application/pdf","application/json"}) public Response gerarBoletoDividaAtivaComCaptcha(@PathParam("processo") String…
-
8
votes1
answer606
viewsError Making Jar Build
I’m trying to do the build of my first JAR using the Intellij and Maven to manage dependencies. I created the artifact and executed build, but when I run the program I get the following exception:…
-
8
votes1
answer355
viewsCentralize (use only one) a Try-catch for every WPF application
Is there any way to centralize a try-catch for every application in a WPF application with C#? I created a window to use as MessageBox customized. When there is an error in any system registration…
-
8
votes1
answer322
viewsHow to turn E_PARSE errors into Exception in PHP?
My question is this:: PHP has a great feature that lets you convert errors that can occur in an application into Exceptions. This can be done through class ErrorException Example:…
-
8
votes1
answer1373
viewsHow to handle duplicate key error?
How do I handle duplicate key error? I need to display message to the user that the "Item is already registered" Something like that: try{ //tenta inserir no Banco de Dados…
-
8
votes1
answer62
viewsDo exceptions cause problems for performance?
When working in an application whose performance is important, NO USE of exceptions. With this comes doubt "what makes the exceptions so bad for performance?" In practical terms what is the need to…
-
8
votes1
answer1205
viewsDo I need to use Try/catch throughout a process chain?
Supposing I have a call in my layer controller for a method in the business layer that leads to another method in the data access layer. Should I use the try/catch in all of them, only in any or…
-
8
votes3
answers15821
viewsREGEX - find expressions that DO NOT contain specific words
We are using REGEX to normalize pharmaceutical data from a string field and we need to distinguish very similar strings from an exception command. For example, in a very simple way, we have the…