Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same Exception Error but different message

Status
Not open for further replies.

eraH

MIS
Dec 1, 2003
106
NZ
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?

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();
        }
    }

}
Thanks again for everyone's help.
 
When you pop the messagebox, you can put any text you want in there. What you do with the exception is a different story.

A good idea would be to log the exception to a log file. This way the end user goes "I couldn't connect" and they can send you the logs to figure out why. Chances are good that the user doesn't know what an index out of bounds exception is or a TCP Stream socket error is. They would rather see "Sorry, Authentication Failed."

 
The problem is, that particular exception occurs if you can't connect to the server or if the username and password is wrong.

This is the message part of the exception if the it can't connection to the server.
"System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it\r\n at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)\r\n at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)\r\n at Tamir.SharpSsh.java.net.Socket..ctor(String host, Int32 port)\r\n at Tamir.SharpSsh.jsch.Util.createSocket(String host, Int32 port, Int32 timeout)"
And this is the exception is the username or password is wrong.
"Auth fail"
I want to display a custom error message depending on the message part of the exception. Or whatever else will get the job done.
 
I got it working using and if else. Not sure why it didn't work the first time I tried it. Ah well.
 
You can use SocketErrorCode property of the SocketException class to determine the type of error.

Use the System.Net.Sockets.SocketErrorCode Enumeration,

e.g.
ex.SocketErrorCode = System.Net.Sockets.SocketError.ConnectionRefused;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top