<p>Is there a good way to find out which exceptions a procedure/function can raise in Delphi (including it's called procedures/functions)? </p>

<p>In Java you always have to declare which exceptions that can be thrown, but this is not the case in Delphi, which could lead to unhandled exceptions. </p>

<p>Are there any code analysis tools that detects unhandled exceptions?</p>

[{'answer_id': 72612, 'author': 'Pierre-Jean Coudert', 'author_id': 8450, 'author_profile': 'https://Stackoverflow.com/users/8450', 'pm_score': 2, 'selected': False, 'text': '<p>Take a look at <a href="http://www.madshi.net/madExceptDescription.htm" rel="nofollow noreferrer">http://www.madshi.net/madExceptDescription.htm</a></p>\n'}, {'answer_id': 72658, 'author': 'PatrickvL', 'author_id': 12170, 'author_profile': 'https://Stackoverflow.com/users/12170', 'pm_score': 3, 'selected': False, 'text': '<p>Except for a scan on the "raise" keyword, there\'s no language construct in Delphi that tells the casual reader which exceptions can be expected from a method.</p>\n\n<p>At runtime, one could add a catch-all exception handler in every method, but that\'s not advisable, as it will slow down the speed of execution. (And it\'s cumbersome to do too).</p>\n\n<p>Adding an exception-handling block to a method will add a few assembly instructions to it (even when the exception isn\'t triggered), which forms measureable slow-down when the method is called very often.</p>\n\n<p>There do exist a few libraries that can help you in analyzing runtime exceptions, like <a href="http://madshi.de/madExceptDescription.htm" rel="nofollow noreferrer">madExcept</a>, <a href="http://jcl.svn.sourceforge.net/viewvc/jcl/trunk/jcl/source/windows/JclDebug.pas?view=markup" rel="nofollow noreferrer">JclDebug</a>, and <a href="http://www.eurekalog.com/" rel="nofollow noreferrer">EurekaLog</a>. These tools can log all kinds of details about the exception, it\'s highly advisable to use one of those!</p>\n'}, {'answer_id': 72670, 'author': 'Ralph M. Rickenbach', 'author_id': 4549416, 'author_profile': 'https://Stackoverflow.com/users/4549416', 'pm_score': 1, 'selected': False, 'text': '<p>For runtime try <a href="http://www.eurekalog.com/" rel="nofollow noreferrer">Eurekalog</a>. I do not know whether a tool exists for design time. You will have more dificoulties even when you have third party code without source. There is no need in Delphi to catch exceptions, so you do not have to declare them like in Java.</p>\n\n<p>What I wanted to say is that Delphi does not require that an exception is handled. It will just terminate the program. EurekaLog provides means to log handled and unhandled exceptions and provide a wealth of information on the sate of the program when the exception occured, including the line of code it occured at and the call stack at the time.</p>\n'}, {'answer_id': 72709, 'author': 'Lars Fosdal', 'author_id': 10002, 'author_profile': 'https://Stackoverflow.com/users/10002', 'pm_score': 2, 'selected': False, 'text': "<p>Any exception not explicitly or generally handled at a specific level will trickle upwards in the call stack.  The Delphi RTL (Run Time Library) will generate a set of different exception classes - (mathematical errors, access errors, class specific errors etc).  You can chose to handle them specifically or generally in the different try except blocks.</p>\n\n<p>You don't really need to declare any new exception classes unless you need to propagate a specific functional context with the exception.</p>\n\n<p>As previous commenters wrote, you can also add a mother of all exception handlers like MadExcept or EurekaLog to catch the uncaught.</p>\n\n<p>edit: This is a blanket insurance against unhandled exceptions</p>\n\n<pre><code>try\n  ThisFunctionMayFail;\nexcept\n  // but it sure won't crash the application\n  on e:exception\n  do begin\n    // something sensible to handle the error \n    // or perhaps log and/or display the the generic e.description message\n  end\nend;\n</code></pre>\n"}, {'answer_id': 72797, 'author': 'Frederik Slijkerman', 'author_id': 12416, 'author_profile': 'https://Stackoverflow.com/users/12416', 'pm_score': 4, 'selected': False, 'text': "<p>My guess is that you're trying to make Delphi behave like Java, which is not a good approach. I'd advise not to worry too much about unhandled exceptions. In the worst case, they'll bubble up to the generic VCL exception handler and cause a Windows message dialog. In a normal application, they won't halt the application.</p>\n\n<p>Well-written code would document the different exceptions that can be raised so you can handle them in a meaningful way. Catch-all handlers aren't recommended since there is really no way to know what to do if you don't know why an exception was raised. I can also highly recommend madExcept.</p>\n"}, {'answer_id': 72839, 'author': 'Graza', 'author_id': 11820, 'author_profile': 'https://Stackoverflow.com/users/11820', 'pm_score': 5, 'selected': True, 'text': '<p>(Edit: It is now obvious that the question referred <em>only</em> to design-time checking.)</p>\n\n<p>New answer:</p>\n\n<p>I cannot state whether there are any tools to check this for you. Pascal Analyzer, for one, does not.</p>\n\n<p>I <em>can</em> tell you, however, that in most Delphi applications, even if there was a tool to check this for you, you would get no results.</p>\n\n<p><em>Why?</em> Because the main message loop in TApplication.Run() wraps all HandleMessage() calls in an exception handling block, which catches all exception types. Thus you will have implicit/default exception handling around 99.999% of code in most applications. And in most applications, this exception handling will be around 100% of your own code - the 0.001% of code which is not wrapped in exception handling will be the automatically generated code.</p>\n\n<p>If there was a tool available to check this for you, you would need to rewrite Application.run() such that it does not include exception handling.</p>\n\n<p>(Previous answer:\n<em>The Application.OnException event handler can be assigned to catch all exceptions that aren\'t handled by other exception handlers. Whilst this is run-time, and thus perhaps not exactly what you are after (it sounds like you want to identify them at design time), it does allow you to trap any exception not handled elsewhere. In conjunction with tools such as the JCLDebug stuff in the <a href="http://sourceforge.net/projects/jcl/" rel="noreferrer">Jedi Code Library</a>, you could log a stack trace to find out where &amp; why an exception occurred, which would allow for further investigation and adding specific exception handling or prevention around the guilty code...</em>)</p>\n'}, {'answer_id': 73176, 'author': 'skamradt', 'author_id': 9217, 'author_profile': 'https://Stackoverflow.com/users/9217', 'pm_score': 2, 'selected': False, 'text': '<p>I will second (or is it third) <a href="http://www.madshi.net/" rel="nofollow noreferrer">MadExcept</a>.  I have been using it successfully in several commercial applications without any problems.  The nice thing about MadExcept is that it will generate a report for you with a full stack trace that will generally point you in the right direction as to what went wrong, and can even include a screenshot, as well has have this automatically emailed to you from the clients computer with a simple mouse click.</p>\n\n<p>However, you don\'t want to use this for ALL exceptions, just to catch the ones you miss.  For instance, if you open a database and the login fails, it would be better for you to catch and handle this one yourself rather than give the user the MadExcept default error in your application occured message.  </p>\n'}, {'answer_id': 73954, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 3, 'selected': False, 'text': "<p>The short answers is there is no tool that does what you say, and even a scan for the <strong>raise</strong> keyword wouldn't get you there.  <em>EAccessViolation</em> or <em>EOutOfMemory</em> are just two of a number of exceptions that could get raised just about anywhere.  </p>\n\n<p>One fundamental thing about Delphi is the exceptions are hierarchical: All defined language exceptions descend from <em>Exception</em>, although it is worth noting that it is actually possible to raise any <em>TObject</em> descendant.</p>\n\n<p>If you want to <strong>catch</strong> every exception that is raised in a particular procedure, just wrap it in a <strong>try / except</strong> block, but as was mentioned <strong><em>this is not recommended</em></strong>.</p>\n\n<pre><code>// Other code . . . \ntry\n  SomeProcedure()\nexcept  // BAD IDEA!\n  ShowMessage('I caught them all!');\nend;\n</code></pre>\n\n<p>That will catch everything, even instances of a raised <em>TObject</em>.  Although I would argue that this is rarely the best course of action.  Usually you want to use a <strong>try / finally</strong> block and then allow your global exception handler (or one final <strong>try / except</strong> block) to actually handle the exceptions.</p>\n"}, {'answer_id': 104594, 'author': 'Frank Shearar', 'author_id': 10259, 'author_profile': 'https://Stackoverflow.com/users/10259', 'pm_score': 1, 'selected': False, 'text': '<p>As Jim McKeeth points out, you can\'t get a definitive answer, but it seems to me that one could <em>partially</em> answer the question by some static analysis: given a particular function/procedure, construct a call graph. Check each of the functions in that call graph for a raise statement. That would tell you, for instance, that TIdTcpClient.ReadString can raise an EIdNotConnected (among others).</p>\n\n<p>A clever analyser might also note that some code uses the / operator and include EDivByZero as a possibility, or that some procedure accesses an array and include ERangeError.</p>\n\n<p>That answer\'s a bit tighter than simply grepping for "raise".</p>\n'}, {'answer_id': 24093419, 'author': 'oOo', 'author_id': 700914, 'author_profile': 'https://Stackoverflow.com/users/700914', 'pm_score': 0, 'selected': False, 'text': "<p>Finalization sections of units can raise exceptions too. These will slip by I think... and are also somewhat problematic.</p>\n<p>I think Delphi IDE has a build-in &quot;stack trace&quot; or &quot;stack tree&quot; something like.</p>\n<p>This question reminds me of Skybuck's TRussianRoulette game... google it, it's code and answer may help.</p>\n"}]
['https://Stackoverflow.com/questions/72562', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11300/']