Error handling

If a command returns a null, there were likely errors. Errors have different severity levels and do not necessarily mean that a command has failed. P4 API.NET methods will throw a P4Exception if an error of severity E_FAILED or higher is returned. This setting can be changed in P4Exception.MinThrowLevel. For example:

Copy
// turn off P4Exceptions
P4Exception.MinThrowLevel = ErrorSeverity.E_NOEXC;

Command that will throw an exception

The following is an example of a command that will throw a P4Exception:

Copy
try
{
    // set options for client command
    ClientCmdOptions clientOpts = new ClientCmdOptions(ClientCmdFlags.Switch);

    // attempt to get label with p4 -s label testLabel
    Label label = rep.GetLabel("testLabel", null, clientOpts);
}
catch (P4Exception ex)
{
    // catch exception and display error message
    Console.WriteLine(ex.Message);
}

An exception is thrown because the error that the command returns is of severity E_FAILED. The Message displayed is: Usage: label [ -d -f -g -i -o -t template ] labelname Invalid option: -s because there is no -s flag for p4 label.

Command that will not throw an exception

This is an example of a command that will not throw a P4Exception:

Copy
// set options for client command
 try
{
    SyncFilesCmdOptions syncOpts = new SyncFilesCmdOptions(SyncFilesCmdFlags.None, 1);
    FileSpec depotFile = new FileSpec(new DepotPath("//depot/test.txt"), null, null, null);
    IList<FileSpec> syncedFiles = rep.Connection.Client.SyncFiles(syncOpts, depotFile);
}
catch (P4Exception ex)
{
    Console.WriteLine(ex.Message);
}

An error is not thrown because the error that the command returns is of severity E_WARN. The file, //depot/test.txt is already at the latest revision but the command has still succeeded with a warning of //depot/test.txt - file(s) up-to-date. syncedFiles will be null.