I've started adding error handling into my program. I've got exception error handling working to a degree. It catches the exception then displays the message part of the exception. But this looks confusing from a end user point of view.
The same exception displays a different message depending on the type of error that's happened. E.g. connection error will display a different message to a authentication error.
How do I create a custom error message?
Thanks again for everyone's help.
The same exception displays a different message depending on the type of error that's happened. E.g. connection error will display a different message to a authentication error.
How do I create a custom error message?
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tamir.SharpSsh;
namespace LMC
{
public partial class MainForm : Form
{
SshExec exec = null;
public MainForm()
{
InitializeComponent();
}
private void logon_button_Click(object sender, EventArgs e)
{
[red] //Connects to server using details entered by user under logon tab
exec = new SshExec(ipaddress_textbox.Text, username_textbox.Text, password_textbox.Text);
//The try and catch bit checks for errors.
try
{
exec.Connect();
//Changes the status label to Green
connectionstatus_label.ForeColor = Color.Green;
connectionstatus_label.Text = "Status: Connected";
}
catch (Tamir.SharpSsh.jsch.JSchException ex)
{
MessageBox.Show(ex.Message);
}
}
[/red]
private void disconnect_button_Click(object sender, EventArgs e)
{
//Closes SSH connection
exec.Close();
//Changes status label back to what it was
connectionstatus_label.ForeColor = Color.Red;
connectionstatus_label.Text = "Status: Disconnected";
}
private void getserverinfo_button_Click(object sender, EventArgs e)
{
//Get version of OS running
string osinfo = "cat /etc/redhat-release";
string osinfo_output = exec.RunCommand(osinfo);
osversion_label.Text = osinfo_output;
//Get amount of RAM in server
string meminfo = "cat /proc/meminfo | grep MemTotal";
string meminfo_output = exec.RunCommand(meminfo);
totalmemory_label.Text = meminfo_output;
//Get CPU info
string cpuinfo = "cat /proc/cpuinfo | grep 'model name'";
string cpuinfo_output = exec.RunCommand(cpuinfo);
cpuinfo_label.Text = cpuinfo_output;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
//if "exec" doesn't have a value then a SSH connection was never made
//Check's that a connection SSH connection was made and closes SSH if there was one made
if (exec != null)
exec.Close();
}
}
}