Re: Exception handling



Sharp Tool wrote:
Hi,

I want to check if a statement throws an exception.
If it does, I want to deal with it then and there, and not at the catch
claus.
See code below:

....
String[] str = {"dinner"};
if (str[1].charAt(0) throws ArrayIndexOutOfBoundsException) // compile error
{
    //do something 1
}
else //do something 2

Is this possible?

Sharp Tool







only by catching or by using a querying method of some kind to determine if an exception is likely to be thrown.

e.g

instead of ...

if (str[1].charAt(0) throws ArrayIndexOutOfBoundsException) // compile error


do...


if (str.length > 0) str[1].charAt(0)

This way - no ArrayIndexOutOfBoundsException will be thrown because of the length is too small we don't try and get the first element.

Google 'writing Exception Safe code'
.



Relevant Pages

  • Re: Validating file names
    ... running Novel, from your G: running Linux file system, from your H: running ... I am not saying one does not exist! ... file file and handle any exceptions that were thrown. ... If an exception is thrown then the specified ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Using an ActiveX ocx control on a Webform
    ... > download the ActiveX I can Automate the ActiveX object through the ... > Exception of type InvalidActiveXStateException was thrown. ... > tried using the item in the design mode of a regular .NET Windows ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Exception-Objekt freigeben
    ... When an exception is thrown, the exception object that is thrown is destroyed ... we have added the AcquireExceptionObject and ReleaseExceptionObject functions. ... then the thrown object is not destroyed by the RTL, but assumed to be in control ...
    (de.comp.lang.delphi.misc)
  • Re: Question about Authorization Manager
    ... They do throw an exception if an empty task is passed for a check. ... second message to be thrown, but I'm guessing that it's a similar ... security block code. ... But I understand that you believe what I was describing was referring to an empty task, i.e. one not containing any operations or nested tasks that contain operations, and this is where we are not in sync with one another. ...
    (microsoft.public.dotnet.security)
  • Re: exception handling in complex Python programs
    ... errors possibly thrown by open, or leave it to the caller? ... def do_something: ... have to couple the exception type with the function --- between file ... Python 3 and an IOError is thrown in the other three cases <bashes ...
    (comp.lang.python)