JaroslavTulach: /* Runtime vs. Checked */ - 2011-03-23 10:49:52

Runtime vs. Checked

←Older revision Revision as of 10:49, 23 March 2011
Line 33: Line 33:
== Runtime vs. Checked ==
== Runtime vs. Checked ==
-
[[Java]] is the first industrial language that introduced [[wikipedia::Checked_exception|checked exceptions]]. As such, when talking about exceptions in context of [[Java]], one cannot escape from talking about runtime vs. checked benefits and drawbacks. However this is tricky, as far as I know this is a perfect topic to start never-ending flamewar. Even the [[wikipedia::Checked_exception|wikipedia]]'s article related to pros and cons is written very defensively (some say.., others mean..., etc.), so it seems important to approach the topic carefully.
+
[[Java]] is the first industrial language that introduced [[Checked exception]]s. As such, when talking about exceptions in context of [[Java]], one cannot escape from talking about runtime vs. checked benefits and drawbacks. However this is tricky, as far as I know this is a perfect topic to start never-ending flamewar. Even the [[wikipedia::Checked_exception|wikipedia]]'s article related to pros and cons is written very defensively (some say.., others mean..., etc.), so it seems important to approach the topic carefully.
There may differences between checked and unchecked exception in comprehensibility, readability or maintainability of the code written against libraries using the first or the latter. However as I argued in [[Runtime_Aspects_of_APIs|Chapter 11]], Runtime Aspects of APIs, from the point of view of API evolution, there is no difference. When a method in a library is written so it can throw some exception in one version and in some newer version decides to throw yet another exception type under some circumstances, then this is an incompatible change. And the change is incompatible in both cases. When using checked exceptions, the change is source incompatible, as one needs to change the signature of the method to define the new exception. As a result code that was compilable, may get broken. In the case of unchecked exceptions, the change is functionally incompatible - as the code which originally caught all exceptions thrown from the method, will no longer work as expected. As such the difference between runtime and checked for API design is not as big as it might have seen.
There may differences between checked and unchecked exception in comprehensibility, readability or maintainability of the code written against libraries using the first or the latter. However as I argued in [[Runtime_Aspects_of_APIs|Chapter 11]], Runtime Aspects of APIs, from the point of view of API evolution, there is no difference. When a method in a library is written so it can throw some exception in one version and in some newer version decides to throw yet another exception type under some circumstances, then this is an incompatible change. And the change is incompatible in both cases. When using checked exceptions, the change is source incompatible, as one needs to change the signature of the method to define the new exception. As a result code that was compilable, may get broken. In the case of unchecked exceptions, the change is functionally incompatible - as the code which originally caught all exceptions thrown from the method, will no longer work as expected. As such the difference between runtime and checked for API design is not as big as it might have seen.

JaroslavTulach: /* Deciding on Importance */ - 2010-05-07 07:54:52

Deciding on Importance

←Older revision Revision as of 07:54, 7 May 2010
Line 67: Line 67:
== Deciding on Importance ==
== Deciding on Importance ==
-
When dealing with flow of exceptions in a complex, modular system, one needs to solve an important problem: ''decide whether an exception is important or not''. This may sound easy, but when you get an exception from a library, how can you tell whether this is something that shall be shown to the user as an information about failed I/O or whether this is an unexpected error state that needs to be logged and workarounded somehow? This is tough. Usual solution builds on the assumption that the top most caller knows the scope of an action (like ''I want to save a file'', ''I want to compile'', etc.) and this top most caller will decide on the importance. This is easy to understand model, and it works well in most situations (especially if there are no exceptional states), however in [[NetBeans]] we needed more granular identification. As such we created a model of annotating an exception with additional attributes:
+
When dealing with flow of exceptions in a complex, [[modular system]], one needs to solve an important problem: ''decide whether an exception is important or not''. This may sound easy, but when you get an exception from a library, how can you tell whether this is something that shall be shown to the user as an information about failed I/O or whether this is an unexpected error state that needs to be logged and workarounded somehow? This is tough. Usual solution builds on the assumption that the top most caller knows the scope of an action (like ''I want to save a file'', ''I want to compile'', etc.) and this top most caller will decide on the importance. This is easy to understand model, and it works well in most situations (especially if there are no exceptional states), however in [[NetBeans]] we needed more granular identification. As such we created a model of annotating an exception with additional attributes:
<source lang="java">
<source lang="java">

JaroslavTulach at 12:41, 15 November 2008 - 2008-11-15 12:41:02

←Older revision Revision as of 12:41, 15 November 2008
Line 88: Line 88:
Exceptions are [[APIDesignPatterns:ExceptionExtensibility|easily extensible]] via subclassing.
Exceptions are [[APIDesignPatterns:ExceptionExtensibility|easily extensible]] via subclassing.
 +
 +
 +
[[Category:APIDesignPatterns]]
 +
[[Category:APIDesignPatterns:Exceptions]]

JaroslavTulach at 18:14, 8 November 2008 - 2008-11-08 18:14:27

←Older revision Revision as of 18:14, 8 November 2008
Line 87: Line 87:
== Extensibility ==
== Extensibility ==
-
It has been mentioned that changing a code to throw a new exception is not compatible change. However this is not fully true, because [[Java]] exceptions are regular classes, and classes support inheritance, one can define new subtypes of existing exceptions and yet keep the code written by your API users compatible. Imagine there is a method in version one throwing ordinary I/O exception:
+
Exceptions are [[APIDesignPatterns:ExceptionExtensibility|easily extensible]] via subclassing.
-
 
+
-
<source lang="java">
+
-
public static int compute(int x, int y) throws IOException {
+
-
if (y - x == 1) throw new IOException("For some reason I cannot deal with this!");
+
-
return x + y;
+
-
}
+
-
</source>
+
-
 
+
-
Now people can use this method to do many things, complex ones or trivial like to sum two numbers:
+
-
 
+
-
<source lang="java">
+
-
int result;
+
-
try {
+
-
result = compute(1, 2);
+
-
} catch (IOException ex) {
+
-
Logger.getLogger("mylogger").log(Level.WARNING, "Problem!", ex);
+
-
result = -1;
+
-
}
+
-
</source>
+
-
 
+
-
This is a valid use of the compute API method. As authors of the library, we value our users and want to support this API use in future. However we also want to please more and more users. If some other ones require us to provide better information about the values of '''x''' and '''y''', instead of just throwing the '''IOException''', can we help them? Can we change the contract and yet pretend we have not changed anything? Yes, this is possible, just imagine version two of the library:
+
-
 
+
-
<source lang="java">
+
-
public static int compute(int x, int y) throws IOException {
+
-
if (y - x == 1) throw new StrangeXYException(x, y);
+
-
return x + y;
+
-
}
+
-
 
+
-
public final class StrangeXYException extends IOException {
+
-
int x, y;
+
-
StrangeXYException(int x, int y) {
+
-
super("For some reason I cannot deal with this!");
+
-
this.x = x;
+
-
this.y = y;
+
-
}
+
-
 
+
-
public int getX() { return x; }
+
-
public int getY() { return y; }
+
-
}
+
-
</source>
+
-
 
+
-
The previously written client code remains valid. A subclass of '''IOException''' is thrown, it is matched by the '''catch (IOException ex)''' block and everything continues to work as it used to. Yet, if one is interested in more detailed information about the failure, one can catch the version two newly defined exception:
+
-
 
+
-
<source lang="java">
+
-
int result;
+
-
try {
+
-
result = compute(1, 2);
+
-
} catch (StrangeXYException ex) {
+
-
// compute ourselves meaningful result
+
-
result = ex.getX() + ex.getY();
+
-
} catch (IOException ex) {
+
-
Logger.getLogger("mylogger").log(Level.WARNING, "Problem!", ex);
+
-
result = -1;
+
-
}
+
-
</source>
+
-
 
+
-
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|switch/case]] where inheritance is not taken into account at all.
+
-
 
+
-
 
+
-
<comments/>
+

JaroslavTulach at 21:38, 14 September 2008 - 2008-09-14 21:38:29

←Older revision Revision as of 21:38, 14 September 2008
Line 145: Line 145:
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|switch/case]] where inheritance is not taken into account at all.
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|switch/case]] where inheritance is not taken into account at all.
 +
 +
 +
<comments/>

JaroslavTulach at 21:32, 14 September 2008 - 2008-09-14 21:32:48

←Older revision Revision as of 21:32, 14 September 2008
Line 144: Line 144:
</source>
</source>
-
The object oriented nature of [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|try/catch]] statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with '''switch/case''' where inheritance is not taken into account at all.
+
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|switch/case]] where inheritance is not taken into account at all.

JaroslavTulach at 21:31, 14 September 2008 - 2008-09-14 21:31:37

←Older revision Revision as of 21:31, 14 September 2008
Line 1: Line 1:
-
TBD: just being created as a response to query by Casper Bang:
+
Casper Bang asked following question after reading the [[TheAPIBook]]:
''I was curious as to know how come, in a book strictly about API design in Java, you do not mention exceptions (particular checked exceptions) and the role they play in documenting assertions vs. hampering versionability. Did you simply think this to be too controversial an issue I wonder?''
''I was curious as to know how come, in a book strictly about API design in Java, you do not mention exceptions (particular checked exceptions) and the role they play in documenting assertions vs. hampering versionability. Did you simply think this to be too controversial an issue I wonder?''
Line 7: Line 7:
== Nothing special ==
== Nothing special ==
-
One reason why there is no special attention paid to exception is that at the end, exceptions are just classes. As such the same rules that can be applied to any class that shows up in the API can be applied to exceptions in the API as well. When adding exceptions in your API you will not do anything bad if you follow the ''do not expose more than necessary'' credo of [[Do_Not_Expose_More_Than_You_Want|Chapter 5]]. If your exception is supposed to be thrown just by your code, it is quite OK to make its constructor package private. That will guarantee the intended purpose of the exception, which is, to be thrown only by you and caught by clients of your API. It will guarantee that nobody can misuse and misinterpret this intention. From the opposite point of view: if you want your clients to throw an exception and only your code to consume it, you do not need public getters to get values passed into the constructor at the time the exception is thrown.
+
One reason why there is no special attention paid to exceptions is that at the end, exceptions are just classes. As such the same rules that can be applied to any class that shows up in the API can be applied to exceptions in the API as well. When adding exceptions in your API you will not do anything bad if you follow the ''do not expose more than necessary'' credo of [[Do_Not_Expose_More_Than_You_Want|Chapter 5]]. If your exception is supposed to be thrown just by your code, it is quite OK to make its constructor package private. That will guarantee the intended purpose of the exception, which is, to be thrown only by you and caught by clients of your API. It will guarantee that nobody can misuse and misinterpret this intention. From the opposite point of view: if you want your clients to throw an exception and only your code to consume it, you do not need public getters to get values passed into the constructor at the time the exception is thrown.
On the other hand, [[Do_Not_Expose_More_Than_You_Want|Chapter 5]] also advices to prefer factory methods over exposing constructors. I tried that few times, but I have a feeling that this feels a bit unnatural and as such I cannot recommend code like:
On the other hand, [[Do_Not_Expose_More_Than_You_Want|Chapter 5]] also advices to prefer factory methods over exposing constructors. I tried that few times, but I have a feeling that this feels a bit unnatural and as such I cannot recommend code like:
Line 29: Line 29:
The common mindshare among Java developers seems to expect that exceptions are raised by writing '''throw new Something''' and it is therefore likely better to expose constructor of your exception class instead of factory method. Still, if you do not expect people to benefit from subclassing your exception, make it '''final''' - your options for [[Ever_Changing_Targets|future evolution]] will remain more open.
The common mindshare among Java developers seems to expect that exceptions are raised by writing '''throw new Something''' and it is therefore likely better to expose constructor of your exception class instead of factory method. Still, if you do not expect people to benefit from subclassing your exception, make it '''final''' - your options for [[Ever_Changing_Targets|future evolution]] will remain more open.
-
In short, exceptions are classes. They shall follow the evolution rules applicable to classes, as discussed in [[Code_Against_Interfaces%2C_Not_Implementations|Chapter 6]]. It is not wise to add abstract methods into exceptions could have been subclassed in prior versions, it is not wise to expose fields, remove elements already available, etc. However Casper is right, this is not all that can be said about exceptions, it seems there is something special at the end.
+
In short, exceptions are classes. They shall follow the evolution rules applicable to classes, as discussed in [[Code_Against_Interfaces%2C_Not_Implementations|Chapter 6]]. It is not wise to add abstract methods into exceptions that could have been subclassed in prior versions, it is not wise to expose fields, remove elements already available, etc. However Casper is right, this is not all that can be said about exceptions, it seems that something special remains unsaid.
== Runtime vs. Checked ==
== Runtime vs. Checked ==
-
[[Java]] is the first industrial language that introduced [[wikipedia::Checked_exception|checked exceptions]]. As such, when talking about exceptions in context of [[Java]], one cannot escape from talking about runtime vs. checked. However this is tricky, as far as I know this is a perfect topic to start never-ending flamewar. Even the [[wikipedia::Checked_exception|wikipedia]]'s article related to pros and cons is written very defensively (some say.., others mean..., etc.), so it seems important to approach the topic carefully.
+
[[Java]] is the first industrial language that introduced [[wikipedia::Checked_exception|checked exceptions]]. As such, when talking about exceptions in context of [[Java]], one cannot escape from talking about runtime vs. checked benefits and drawbacks. However this is tricky, as far as I know this is a perfect topic to start never-ending flamewar. Even the [[wikipedia::Checked_exception|wikipedia]]'s article related to pros and cons is written very defensively (some say.., others mean..., etc.), so it seems important to approach the topic carefully.
-
There may differences between checked and unchecked exception in comprehensibility, readability or maintainability of the code written against libraries using the first or the latter. However as I argued in [[Runtime_Aspects_of_APIs|Chapter 11]], Runtime Aspects of APIs, from the point of view of API evolution, there is no difference. If a method in a library can throw some exception in one version and in some newer version decides to throw yet another exception type under some circumstances, then this is incompatible change. And the change is incompatible in both cases. When using checked exceptions, the change is binary incompatible, as one needs to change the signature of the method to define the new exception. In case of unchecked exceptions, the change is functionally incompatible - as the code which originally caught all exceptions thrown from the method, will no longer work as expected. As such the difference between runtime and checked for API design is not as big as it might have seen.
+
There may differences between checked and unchecked exception in comprehensibility, readability or maintainability of the code written against libraries using the first or the latter. However as I argued in [[Runtime_Aspects_of_APIs|Chapter 11]], Runtime Aspects of APIs, from the point of view of API evolution, there is no difference. When a method in a library is written so it can throw some exception in one version and in some newer version decides to throw yet another exception type under some circumstances, then this is an incompatible change. And the change is incompatible in both cases. When using checked exceptions, the change is source incompatible, as one needs to change the signature of the method to define the new exception. As a result code that was compilable, may get broken. In the case of unchecked exceptions, the change is functionally incompatible - as the code which originally caught all exceptions thrown from the method, will no longer work as expected. As such the difference between runtime and checked for API design is not as big as it might have seen.
== My Single Exception ==
== My Single Exception ==
Line 42: Line 42:
<source lang="java">
<source lang="java">
 +
try {
 +
// do some operations
} catch (IOException ex) {
} catch (IOException ex) {
throw new BuildException(oldEx);
throw new BuildException(oldEx);
Line 47: Line 49:
</source>
</source>
-
This is quite irritating verbosity, if we keep in mind that almost every task needs to deal with I/O. Also the final error reports are not really easy to read - for one I/O failure, there are two exceptions chained to each other. Moreover only the inner one is important, the other is quite useless. Things can get even messier when you imagine that some maven goals are wrappers around Ant tasks. As such one gets chain of at least three exceptions, as maven goal needs to wrap the Ant's task invocation and the tasks wraps the I/O.
+
This is overcomingly verbose, especially if we keep in mind that almost every task needs to deal with I/O. Also the final error reports are not really easy to read - for one I/O failure, there are two exceptions chained to each other. Moreover only the inner one is important, the other is quite useless. Things can get even messier when you imagine that some maven goals are wrappers around Ant tasks. As such one gets chain of at least three exceptions, as maven goal needs to wrap the Ant's task invocation and the tasks wraps the I/O.
It would be much simpler if both [[Ant]] and [[Maven]] designers admitted that it is natural to throw '''IOException''' from inside its tasks/goal implementations. Then there could be:
It would be much simpler if both [[Ant]] and [[Maven]] designers admitted that it is natural to throw '''IOException''' from inside its tasks/goal implementations. Then there could be:
Line 65: Line 67:
== Deciding on Importance ==
== Deciding on Importance ==
-
When dealing with flow of exceptions in a complex, modular system, one needs to solve an important problem: ''decide whether an exception is important or not''. This may sound easy, but when you get an exception from a library, how can you tell whether this is something that shall be shown to the user as an information about failed I/O or whether this is an unexpected error state that needs to be just logged? This is tough. Usual solution counts that the top most caller knows the scope of an action (like ''I want to save a file'', ''I want to compile'', etc.) and this top most caller will decide on the importance. This is easy to understand model, and it works well in most situations (especially if there are no exceptional states), however in [[NetBeans]] we needed more granular identification. As such we created a model of annotating an exception with additional attributes:
+
When dealing with flow of exceptions in a complex, modular system, one needs to solve an important problem: ''decide whether an exception is important or not''. This may sound easy, but when you get an exception from a library, how can you tell whether this is something that shall be shown to the user as an information about failed I/O or whether this is an unexpected error state that needs to be logged and workarounded somehow? This is tough. Usual solution builds on the assumption that the top most caller knows the scope of an action (like ''I want to save a file'', ''I want to compile'', etc.) and this top most caller will decide on the importance. This is easy to understand model, and it works well in most situations (especially if there are no exceptional states), however in [[NetBeans]] we needed more granular identification. As such we created a model of annotating an exception with additional attributes:
<source lang="java">
<source lang="java">
Line 85: Line 87:
== Extensibility ==
== Extensibility ==
-
It has been mentioned that changing a code to throw a new exception is not compatible change. However this is not fully true, because [[Java]] exceptions are regular classes, and classes support inheritance, one can define new subtypes of existing exceptions and yet the code remains compatible. Imagine there is a method in version one throwing ordinary I/O exception:
+
It has been mentioned that changing a code to throw a new exception is not compatible change. However this is not fully true, because [[Java]] exceptions are regular classes, and classes support inheritance, one can define new subtypes of existing exceptions and yet keep the code written by your API users compatible. Imagine there is a method in version one throwing ordinary I/O exception:
<source lang="java">
<source lang="java">
Line 94: Line 96:
</source>
</source>
-
Now people can use this method to sum two numbers:
+
Now people can use this method to do many things, complex ones or trivial like to sum two numbers:
<source lang="java">
<source lang="java">
Line 106: Line 108:
</source>
</source>
-
This is a valid use of the compute API method. As authors of the library, we value our users and want to support this API use in future. However some other users require us to provide better information about the values of '''x''' and '''y''', instead of just throwing the '''IOException'''. Can we change the contract and yet pretend we have not changed anything? Yes, that is possible, imagine version two of the library:
+
This is a valid use of the compute API method. As authors of the library, we value our users and want to support this API use in future. However we also want to please more and more users. If some other ones require us to provide better information about the values of '''x''' and '''y''', instead of just throwing the '''IOException''', can we help them? Can we change the contract and yet pretend we have not changed anything? Yes, this is possible, just imagine version two of the library:
<source lang="java">
<source lang="java">
Line 142: Line 144:
</source>
</source>
-
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than the one available with '''switch/case''' where inheritance is not taken into account at all.
+
The object oriented nature of [[Talk:Blogs:AndreiBadea:EnumsInAPIs#Joel_Neely_said_...|try/catch]] statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than for example the one available with '''switch/case''' where inheritance is not taken into account at all.

JaroslavTulach: /* Extensibility */ - 2008-09-14 21:13:18

Extensibility

←Older revision Revision as of 21:13, 14 September 2008
Line 85: Line 85:
== Extensibility ==
== Extensibility ==
-
TBD: Subclassing. Compare with switch/case
+
It has been mentioned that changing a code to throw a new exception is not compatible change. However this is not fully true, because [[Java]] exceptions are regular classes, and classes support inheritance, one can define new subtypes of existing exceptions and yet the code remains compatible. Imagine there is a method in version one throwing ordinary I/O exception:
 +
 
 +
<source lang="java">
 +
public static int compute(int x, int y) throws IOException {
 +
if (y - x == 1) throw new IOException("For some reason I cannot deal with this!");
 +
return x + y;
 +
}
 +
</source>
 +
 
 +
Now people can use this method to sum two numbers:
 +
 
 +
<source lang="java">
 +
int result;
 +
try {
 +
result = compute(1, 2);
 +
} catch (IOException ex) {
 +
Logger.getLogger("mylogger").log(Level.WARNING, "Problem!", ex);
 +
result = -1;
 +
}
 +
</source>
 +
 
 +
This is a valid use of the compute API method. As authors of the library, we value our users and want to support this API use in future. However some other users require us to provide better information about the values of '''x''' and '''y''', instead of just throwing the '''IOException'''. Can we change the contract and yet pretend we have not changed anything? Yes, that is possible, imagine version two of the library:
 +
 
 +
<source lang="java">
 +
public static int compute(int x, int y) throws IOException {
 +
if (y - x == 1) throw new StrangeXYException(x, y);
 +
return x + y;
 +
}
 +
 
 +
public final class StrangeXYException extends IOException {
 +
int x, y;
 +
StrangeXYException(int x, int y) {
 +
super("For some reason I cannot deal with this!");
 +
this.x = x;
 +
this.y = y;
 +
}
 +
 
 +
public int getX() { return x; }
 +
public int getY() { return y; }
 +
}
 +
</source>
 +
 
 +
The previously written client code remains valid. A subclass of '''IOException''' is thrown, it is matched by the '''catch (IOException ex)''' block and everything continues to work as it used to. Yet, if one is interested in more detailed information about the failure, one can catch the version two newly defined exception:
 +
 
 +
<source lang="java">
 +
int result;
 +
try {
 +
result = compute(1, 2);
 +
} catch (StrangeXYException ex) {
 +
// compute ourselves meaningful result
 +
result = ex.getX() + ex.getY();
 +
} catch (IOException ex) {
 +
Logger.getLogger("mylogger").log(Level.WARNING, "Problem!", ex);
 +
result = -1;
 +
}
 +
</source>
 +
 
 +
The object oriented nature of '''try/catch''' statements makes evolution perfectly possible. With every new release one can define new specialized exception as subclass of some already existing one. The previously working code can remain unaffected, the new client code can extract the additional information from the new exception class exposed in the API. This is much more pleasant evolution than the one available with '''switch/case''' where inheritance is not taken into account at all.

JaroslavTulach: /* Deciding on Importance */ - 2008-09-14 20:53:33

Deciding on Importance

←Older revision Revision as of 20:53, 14 September 2008
Line 65: Line 65:
== Deciding on Importance ==
== Deciding on Importance ==
-
TBD: [[NetBeans]]' ErrorManager and Exceptions classes.
+
When dealing with flow of exceptions in a complex, modular system, one needs to solve an important problem: ''decide whether an exception is important or not''. This may sound easy, but when you get an exception from a library, how can you tell whether this is something that shall be shown to the user as an information about failed I/O or whether this is an unexpected error state that needs to be just logged? This is tough. Usual solution counts that the top most caller knows the scope of an action (like ''I want to save a file'', ''I want to compile'', etc.) and this top most caller will decide on the importance. This is easy to understand model, and it works well in most situations (especially if there are no exceptional states), however in [[NetBeans]] we needed more granular identification. As such we created a model of annotating an exception with additional attributes:
 +
 
 +
<source lang="java">
 +
public final class Exceptions {
 +
private Exceptions() { }
 +
 
 +
public static <T extends Throwable> T attachMessage(T ex, String msg);
 +
public string <T extends Throwable> T attachLocalizedMessage(T ex, String msg);
 +
public string <T extends Throwable> T attachImportance(T ex, java.util.Level level);
 +
 
 +
public static Level findImportance(Throwable t);
 +
public static String findMessage(Throwable t);
 +
public static String findLocalizedMessage(Throwable t);
 +
}
 +
</source>
 +
 
 +
More info at [http://bits.netbeans.org/6.1/javadoc/org-openide-util/org/openide/util/doc-files/logging.html logging FAQ]. As a result any code on the exception thrown chain can jump in and mark the exception as important or unimportant and pass it on. The final '''catch''' block then gets the importance information and uses it decide whether show a dialog to the user, or ''swallow'' the exception.
== Extensibility ==
== Extensibility ==
TBD: Subclassing. Compare with switch/case
TBD: Subclassing. Compare with switch/case

JaroslavTulach: /* My Single Exception */ - 2008-09-14 20:37:56

My Single Exception

←Older revision Revision as of 20:37, 14 September 2008
Line 49: Line 49:
This is quite irritating verbosity, if we keep in mind that almost every task needs to deal with I/O. Also the final error reports are not really easy to read - for one I/O failure, there are two exceptions chained to each other. Moreover only the inner one is important, the other is quite useless. Things can get even messier when you imagine that some maven goals are wrappers around Ant tasks. As such one gets chain of at least three exceptions, as maven goal needs to wrap the Ant's task invocation and the tasks wraps the I/O.
This is quite irritating verbosity, if we keep in mind that almost every task needs to deal with I/O. Also the final error reports are not really easy to read - for one I/O failure, there are two exceptions chained to each other. Moreover only the inner one is important, the other is quite useless. Things can get even messier when you imagine that some maven goals are wrappers around Ant tasks. As such one gets chain of at least three exceptions, as maven goal needs to wrap the Ant's task invocation and the tasks wraps the I/O.
-
It would be much simpler if both [[Ant]] and [[Maven]] designers
+
It would be much simpler if both [[Ant]] and [[Maven]] designers admitted that it is natural to throw '''IOException''' from inside its tasks/goal implementations. Then there could be:
 +
 
 +
<source lang="java">
 +
 
 +
public abstract class Task {
 +
public abstract void execute() throws IOException;
 +
}
 +
 
 +
public class BuildException extends IOException {
 +
}
 +
</source>
 +
 
 +
The code inside custom '''execute''' implementation could be shorter, as there would be no need for the '''try/catch''' of I/O operation exceptions, users would get no wrapped I/O exceptions and the expressiveness would remain the same, as the possibility to raise the '''BuildException''' would be kept.
== Deciding on Importance ==
== Deciding on Importance ==