Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I'm reading the book 'Clean Code' of Robert C. Martin and he strongly recommend to "extract the bodies of the try and catch blocks out into functions of their own"
Here is the book example, to make it clear:
public void delete(Page page)
{
try
{
deletePageAndAllReferences(page);
}
catch (Exception e)
{
logError(e);
}
}
private void deletePageAndAllReferences(Page page) throws Exception {
deletePage(page);
registry.deleteReference(page.name);
configKeys.deleteKey(page.name.makeKey());
}
private void logError(Exception e) {
logger.log(e.getMessage());
}
The reasons to do that are:
try/catch blocks confuse the structure of the code and mix error processing with normal processing
nice separation that makes the code easier to understand and modify.
The thing is, I've been working for a few years over several project and this never was a rule, and I didn't find people who follow this even in environments where they really care about clean code.
So I want to know:
The book examples are based on Java and I'm working with C#/.NET, there is any standard or convention coming from Microsoft of from the .NET community about this?
One reason that I can think of is what if the catch block had more than one line to it. For example if you were logging the error, sending a notification email, and rolling back a database transaction or something. If you start adding other things to the catch block you'll find yourself having to repeat code.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am pretty new to Java. I learned a lot this month by making Minecraft plugins and I'd like to try making a new type of plugin:
a plugin that blocks messages incoming from another plugin if them contain a certain word or sentence.
How can I achieve that? I'm asking this because there are lot of plugins with hardcoded messages and people who don't know how to code cannot change them. I am making some addon plugins for many other plugins to help people customizing messages. Any help would be highly appreciated.
Yes, that's possible. You should intercept the message you don't want using packet handling.
You can do it directly from the Bukkit server or on the Bungeecord proxy layer.
To make things easy, you can use ProtocolLib. I've not been working with Minecraft for a long time, I'm not sure how different will be that library but I let you here a piece of code to let you search on it.
ProtocolLibrary.getProtocolManager().addPacketListener(
new PacketAdapter(this, ConnectionSide.SERVER_SIDE, Packets.Server.CHAT) {
#Override
public void onPacketSending(PacketEvent event) {
String message = event.getPacket().getStrings().read(0);
event.getPacket().getStrings().write(0, "Intercepted: "+message);
}
}
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm working on a java project and I want to attach a calculator from the operating system. But I haven't any idea to do that. If there any idea it would be a great help.
You can use
java.lang.Runtime.getRuntime().exec("[path]\calc.exe");
To open the calculator. But beware: The only way that you can communicate with applications you open this way is through the Process streams: in/out/err (assuming calc is set up to communicate as such)
in cmd you can type calc.exe and run it, to run calculator through the java use the java.lang.Runtime class to run commands:
try {
Runtime.getRuntime().exec("calc.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
more details about Shell Commands in java read this...
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a method entangled with control structures. It has many ways to exit. Before leaving the method I need to do some final processing. Instead of repeating the same logic before each exit or refactoring that logic in a method and calling it several times it seem handy to leave that in a finally block. Is it really a legitimate use of finally or am I abusing it?
finally is there for a reason, to add logic that must be execute before the exiting block
It's a valid choice for a method if you don't want/need to use AOP/AspectJ
Notice you may have to use finally for release resources as Connection
For example you can use it when you must audit/log or do autonomous transaction at the end of the method
As #DaveNewton comment, in some cases there might be a better way of refactoring/separating logic, but you can't ignore that it's a valid usage
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I did a little experiment to try to arrange the order of the printing of sysout and syserr to the console because it's mixing sometimes. I tried this:
System.out.println("output");
Thread.sleep(100); //Wait before printing the error to insure the sysout comes first.
System.err.println("Error");
and it worked fine. But I read questions here about sysout and syserr printing out of order and this is not suggested. I'm just wondering is using thread.sleep(); in situations like this bad? I'm using eclipse and in my project I put thread.sleep before every syserr in my code.
Do a flush() instead of a sleep().
System.out.println("whatever");
System.out.flush();
...
(added in response to OP's comments)
Within the single thread a cross-mix of printing cannot happen.
It must be that one of the other threads in the five other places is printing at the same time. You'll need to synchronize on something. e.g., write a little utility method
public static void printToOutAndErr(String toOut, String toErr) {
synchronized(System.out) {
System.out.println(toOut);
System.err.println(toErr);
}
}
And have them all use that.
However, let me strongly strongly suggest that you look into a logging framework instead. They cover all this stuff for you.
As you don't appear to be using more than a single thread, that should work fine. A Concurrency refresher may be of help.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I just wonder about best way to write custom exception name, like if I have user and i want make exception for add and delete and update, what is better using names like :
UserAddException
UserUpdateException
UserRemoveException or UserDeleteException ?
or like:
UserAdditionException
UserUpdateException
UserDeletionException
Exception name must describe what it handle, but not sure use "verb" describe the action where exception happened or "noun" as what exception itself do.
What I want to understand here the best way that make developers understand my exception usage and what to use later, and if there is pattern or standard used for Java development in this case.
I would go with the second type of exception names. The reason why I would say that is because the exception: InstantiationException uses the noun InstantiationException. However, he most important thing is that you are consistent with the naming of exceptions and that the exception names give the development team a clear idea of what those exceptions indicate and their meaning. That is really the critical thing here.
What I would do will be, use UserManagementException instead of too many names, and specify the exact cause of it in some message or error code defined additionally in the class.