Skip to content

Commit f9db6e3

Browse files
author
Matthias Radestock
committed
remove superfluos try/catch
1 parent daf6e7a commit f9db6e3

File tree

12 files changed

+204
-258
lines changed

12 files changed

+204
-258
lines changed

projects/examples/client/AddClient/src/examples/AddClient.cs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,33 @@
6565
namespace RabbitMQ.Client.Examples {
6666
public class AddClient {
6767
public static int Main(string[] args) {
68-
try {
69-
if (args.Length < 1) {
70-
Console.Error.WriteLine("Usage: AddClient <hostname>[:<portnumber>] [<number> ...]");
71-
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
72-
return 1;
73-
}
74-
75-
using (IConnection conn = new ConnectionFactory().CreateConnection(args[0])) {
76-
using (IModel ch = conn.CreateModel()) {
77-
78-
object[] addends = new object[args.Length - 1];
79-
for (int i = 0; i < args.Length - 1; i++) {
80-
addends[i] = double.Parse(args[i + 1]);
81-
}
68+
if (args.Length < 1) {
69+
Console.Error.WriteLine("Usage: AddClient <hostname>[:<portnumber>] [<number> ...]");
70+
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
71+
return 2;
72+
}
73+
74+
using (IConnection conn = new ConnectionFactory().CreateConnection(args[0])) {
75+
using (IModel ch = conn.CreateModel()) {
76+
77+
object[] addends = new object[args.Length - 1];
78+
for (int i = 0; i < args.Length - 1; i++) {
79+
addends[i] = double.Parse(args[i + 1]);
80+
}
8281

83-
SimpleRpcClient client = new SimpleRpcClient(ch, "AddServer");
84-
client.TimeoutMilliseconds = 5000;
85-
client.TimedOut += new EventHandler(TimedOutHandler);
86-
client.Disconnected += new EventHandler(DisconnectedHandler);
87-
object[] reply = client.Call(addends);
88-
if (reply == null) {
89-
Console.WriteLine("Timeout or disconnection.");
90-
} else {
91-
Console.WriteLine("Reply: {0}", reply[0]);
92-
}
82+
SimpleRpcClient client = new SimpleRpcClient(ch, "AddServer");
83+
client.TimeoutMilliseconds = 5000;
84+
client.TimedOut += new EventHandler(TimedOutHandler);
85+
client.Disconnected += new EventHandler(DisconnectedHandler);
86+
object[] reply = client.Call(addends);
87+
if (reply == null) {
88+
Console.WriteLine("Timeout or disconnection.");
89+
} else {
90+
Console.WriteLine("Reply: {0}", reply[0]);
9391
}
9492
}
95-
return 0;
96-
} catch (Exception e) {
97-
Console.Error.WriteLine(e);
98-
return 2;
9993
}
94+
return 0;
10095
}
10196

10297
public static void TimedOutHandler(object sender, EventArgs e) {

projects/examples/client/AddServer/src/examples/AddServer.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,19 @@
6666
namespace RabbitMQ.Client.Examples {
6767
public class AddServer: SimpleRpcServer {
6868
public static int Main(string[] args) {
69-
try {
70-
if (args.Length < 1) {
71-
Console.Error.WriteLine("Usage: AddServer <hostname>[:<portnumber>]");
72-
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
73-
return 1;
74-
}
69+
if (args.Length < 1) {
70+
Console.Error.WriteLine("Usage: AddServer <hostname>[:<portnumber>]");
71+
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
72+
return 2;
73+
}
7574

76-
using (IConnection conn = new ConnectionFactory().CreateConnection(args[0])) {
77-
using (IModel ch = conn.CreateModel()) {
78-
Subscription sub = new Subscription(ch, "AddServer");
79-
new AddServer(sub).MainLoop();
80-
}
75+
using (IConnection conn = new ConnectionFactory().CreateConnection(args[0])) {
76+
using (IModel ch = conn.CreateModel()) {
77+
Subscription sub = new Subscription(ch, "AddServer");
78+
new AddServer(sub).MainLoop();
8179
}
82-
return 0;
83-
} catch (Exception e) {
84-
Console.Error.WriteLine(e);
85-
return 2;
8680
}
81+
return 0;
8782
}
8883

8984
public AddServer(Subscription sub): base(sub) {}

projects/examples/client/DeclareQueue/src/examples/DeclareQueue.cs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,41 +67,40 @@
6767
namespace RabbitMQ.Client.Examples {
6868
public class DeclareQueue {
6969
public static int Main(string[] args) {
70-
try {
71-
int optionIndex = 0;
72-
bool durable = false;
73-
bool delete = false;
74-
IDictionary arguments = null;
75-
while (optionIndex < args.Length) {
76-
if (args[optionIndex] == "/durable") { durable = true; }
77-
else if (args[optionIndex] == "/delete") { delete = true; }
78-
else if (args[optionIndex].StartsWith("/arg:")) {
79-
if (arguments == null) { arguments = new Hashtable(); }
80-
string[] pieces = args[optionIndex].Split(new Char[] { ':' });
81-
if (pieces.Length >= 3) {
82-
arguments[pieces[1]] = pieces[2];
83-
}
70+
int optionIndex = 0;
71+
bool durable = false;
72+
bool delete = false;
73+
IDictionary arguments = null;
74+
while (optionIndex < args.Length) {
75+
if (args[optionIndex] == "/durable") { durable = true; }
76+
else if (args[optionIndex] == "/delete") { delete = true; }
77+
else if (args[optionIndex].StartsWith("/arg:")) {
78+
if (arguments == null) { arguments = new Hashtable(); }
79+
string[] pieces = args[optionIndex].Split(new Char[] { ':' });
80+
if (pieces.Length >= 3) {
81+
arguments[pieces[1]] = pieces[2];
8482
}
85-
else { break; }
86-
optionIndex++;
8783
}
84+
else { break; }
85+
optionIndex++;
86+
}
8887

89-
if (((args.Length - optionIndex) < 2) ||
90-
(((args.Length - optionIndex) % 2) != 0))
88+
if (((args.Length - optionIndex) < 2) ||
89+
(((args.Length - optionIndex) % 2) != 0))
9190
{
9291
Console.Error.WriteLine("Usage: DeclareQueue [<option> ...] <hostname>[:<portnumber>] <queue> [<exchange> <routingkey>] ...");
9392
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
9493
Console.Error.WriteLine("Available options:");
9594
Console.Error.WriteLine(" /durable declare a durable queue");
9695
Console.Error.WriteLine(" /delete delete after declaring");
9796
Console.Error.WriteLine(" /arg:KEY:VAL add longstr entry to arguments table");
98-
return 1;
97+
return 2;
9998
}
10099

101-
string serverAddress = args[optionIndex++];
102-
string inputQueueName = args[optionIndex++];
100+
string serverAddress = args[optionIndex++];
101+
string inputQueueName = args[optionIndex++];
103102

104-
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
103+
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
105104
{
106105
using (IModel ch = conn.CreateModel()) {
107106

@@ -124,10 +123,6 @@ public static int Main(string[] args) {
124123
return 0;
125124
}
126125
}
127-
} catch (Exception e) {
128-
Console.Error.WriteLine(e);
129-
return 2;
130-
}
131126
}
132127
}
133128
}

projects/examples/client/ExceptionTest/src/examples/ExceptionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static int Main(string[] args) {
7070
if (args.Length < 1) {
7171
Console.Error.WriteLine("Usage: ExceptionTest <hostname>[:<portnumber>]");
7272
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
73-
return 1;
73+
return 2;
7474
}
7575

7676
string serverAddress = args[0];
@@ -100,7 +100,7 @@ public static int Main(string[] args) {
100100
} catch (Exception e) {
101101
Console.Error.WriteLine("-=-=-=-=-= MAIN EXCEPTION CATCHER");
102102
Console.Error.WriteLine(e);
103-
return 2;
103+
return 1;
104104
}
105105
}
106106

projects/examples/client/LogTail/src/examples/LogTail.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,20 @@
6767
namespace RabbitMQ.Client.Examples {
6868
public class LogTail {
6969
public static int Main(string[] args) {
70-
try {
71-
if (args.Length < 4) {
72-
Console.Error.WriteLine("Usage: LogTail <hostname>[:<portnumber>] <exchange> <exchangetype> <routingkey>");
73-
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
74-
Console.Error.WriteLine("If the exchange name is the empty string, will instead declare a queue named");
75-
Console.Error.WriteLine("by the routingkey, and consume from that queue.");
76-
return 1;
77-
}
70+
if (args.Length < 4) {
71+
Console.Error.WriteLine("Usage: LogTail <hostname>[:<portnumber>] <exchange> <exchangetype> <routingkey>");
72+
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
73+
Console.Error.WriteLine("If the exchange name is the empty string, will instead declare a queue named");
74+
Console.Error.WriteLine("by the routingkey, and consume from that queue.");
75+
return 2;
76+
}
7877

79-
string serverAddress = args[0];
80-
string exchange = args[1];
81-
string exchangeType = args[2];
82-
string routingKey = args[3];
78+
string serverAddress = args[0];
79+
string exchange = args[1];
80+
string exchangeType = args[2];
81+
string routingKey = args[3];
8382

84-
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
83+
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
8584
{
8685
using (IModel ch = conn.CreateModel()) {
8786
Subscription sub;
@@ -104,10 +103,6 @@ public static int Main(string[] args) {
104103
return 0;
105104
}
106105
}
107-
} catch (Exception e) {
108-
Console.Error.WriteLine(e);
109-
return 2;
110-
}
111106
}
112107

113108
public static void ProcessSingleDelivery(BasicDeliverEventArgs e) {

projects/examples/client/LowlevelLogTail/src/examples/LowlevelLogTail.cs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// This source code is dual-licensed under the Apache License, version
23
// 2.0, and the Mozilla Public License, version 1.1.
34
//
@@ -67,52 +68,47 @@
6768
namespace RabbitMQ.Client.Examples {
6869
public class LowlevelLogTail {
6970
public static int Main(string[] args) {
70-
try {
71-
if (args.Length < 4) {
72-
Console.Error.WriteLine("Usage: LowlevelLogTail <hostname>[:<portnumber>] <exchange> <exchangetype> <routingkey>");
73-
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
74-
Console.Error.WriteLine("If the exchange name is the empty string, will instead declare a queue named");
75-
Console.Error.WriteLine("by the routingkey, and consume from that queue.");
76-
return 1;
77-
}
71+
if (args.Length < 4) {
72+
Console.Error.WriteLine("Usage: LowlevelLogTail <hostname>[:<portnumber>] <exchange> <exchangetype> <routingkey>");
73+
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
74+
Console.Error.WriteLine("If the exchange name is the empty string, will instead declare a queue named");
75+
Console.Error.WriteLine("by the routingkey, and consume from that queue.");
76+
return 2;
77+
}
7878

79-
string serverAddress = args[0];
80-
string exchange = args[1];
81-
string exchangeType = args[2];
82-
string routingKey = args[3];
79+
string serverAddress = args[0];
80+
string exchange = args[1];
81+
string exchangeType = args[2];
82+
string routingKey = args[3];
8383

84-
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
84+
using (IConnection conn = new ConnectionFactory().CreateConnection(serverAddress))
8585
{
8686
using (IModel ch = conn.CreateModel())
87-
{
88-
string queueName;
89-
if (exchange == "") {
90-
ch.QueueDeclare(routingKey);
91-
queueName = routingKey;
92-
} else {
93-
ch.ExchangeDeclare(exchange, exchangeType);
94-
queueName = ch.QueueDeclare();
95-
ch.QueueBind(queueName, exchange, routingKey, false, null);
96-
}
87+
{
88+
string queueName;
89+
if (exchange == "") {
90+
ch.QueueDeclare(routingKey);
91+
queueName = routingKey;
92+
} else {
93+
ch.ExchangeDeclare(exchange, exchangeType);
94+
queueName = ch.QueueDeclare();
95+
ch.QueueBind(queueName, exchange, routingKey, false, null);
96+
}
9797

98-
MyConsumer consumer = new MyConsumer(ch);
99-
ch.BasicConsume(queueName, null, consumer);
98+
MyConsumer consumer = new MyConsumer(ch);
99+
ch.BasicConsume(queueName, null, consumer);
100100

101-
Console.WriteLine("Consumer tag: " + consumer.ConsumerTag);
101+
Console.WriteLine("Consumer tag: " + consumer.ConsumerTag);
102102

103-
while (consumer.IsRunning) {
104-
// Dummy main thread. Often, this will be
105-
// a GUI thread or similar.
106-
Thread.Sleep(500);
107-
}
103+
while (consumer.IsRunning) {
104+
// Dummy main thread. Often, this will be
105+
// a GUI thread or similar.
106+
Thread.Sleep(500);
107+
}
108108

109-
return 0;
110-
}
109+
return 0;
110+
}
111111
}
112-
} catch (Exception e) {
113-
Console.Error.WriteLine(e);
114-
return 2;
115-
}
116112
}
117113

118114
///<summary>Subclass of the very low-level DefaultBasicConsumer</summary>

projects/examples/client/PerfTest/src/examples/PerfTest.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,17 @@ public class PerfTest {
6666
private static readonly byte[] Message = new byte[0];
6767

6868
public static int Main(string[] args) {
69-
try {
70-
if (args.Length < 2) {
71-
Console.Error.WriteLine("Usage: PerfTest <hostname>[:<portnumber>] <number of messages>");
72-
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
73-
return 1;
74-
}
69+
if (args.Length < 2) {
70+
Console.Error.WriteLine("Usage: PerfTest <hostname>[:<portnumber>] <number of messages>");
71+
Console.Error.WriteLine("RabbitMQ .NET client version "+typeof(IModel).Assembly.GetName().Version.ToString());
72+
return 2;
73+
}
7574

76-
string serverAddress = args[0];
77-
int messageCount = int.Parse(args[1]);
75+
string serverAddress = args[0];
76+
int messageCount = int.Parse(args[1]);
7877

79-
using (IConnection conn =
80-
new ConnectionFactory().CreateConnection(serverAddress))
78+
using (IConnection conn =
79+
new ConnectionFactory().CreateConnection(serverAddress))
8180
{
8281
Stopwatch sendTimer = new Stopwatch();
8382
Stopwatch receiveTimer = new Stopwatch();
@@ -117,10 +116,6 @@ public static int Main(string[] args) {
117116

118117
return 0;
119118
}
120-
} catch (Exception e) {
121-
Console.Error.WriteLine(e);
122-
return 2;
123-
}
124119
}
125120

126121
private static double ToHertz(long milliseconds, int messageCount) {

0 commit comments

Comments
 (0)