I am writing a youtube upload software. Actually my question is generic here.
The Google.GData.Client produces an exception. But I don't know how to reach in order to write ?
I mean how do I access it ? I tried with E. but there is no Google.Gdata
I need to access Google.GData.Client.GDataRequestException.ResponceString
You need to change your catch clause to specify the type of exception (in your case, Google.GData.Client.GDataRequestException) so that you can access its members.
catch (Google.GData.Client.GDataRequestException ex)
{
Console.WriteLine(ex.ResponseString);
}
try {
// your GDataRequest code goes here
} catch (GDataRequestException e ) {
// your error code goes here
}
Related
I'm trying to move a file from A to B via ftp like this:
ftpClient.Rename(sourcePathName, targetPathName);
I want to catch and handle all exceptions which occur when the file which has to be moved isn't found. However Rename throws the generic exception FtpCommandException with value {"file/directory not found"}. Unfortunately this exception is thrown in some other cases.
I don't feel that comparing the value of an exception is a clean approach like:
if("file/directory not found".equals(exception.value)) ...
C# 6 onwards, You can use exception filtering
try
{
// your code
}
catch(FtpCommandException ex)
when (ex.Value == "file/directory not found")
{
// do something with this exception
}
Pre C#6 your only option was a condition inside the catch:
try
{
// your code
}
catch(FtpCommandException ex)
{
if(ex.Value == "file/directory not found")
{
// do something with this exception
}
}
Maybe you should check if the file exists before attempting the rename:
if (ftpClient.FileExists(sourcePathName)){
ftpClient.Rename(sourcePathName, targetPathName);
}
I have this piece of code :
try{
// other instructions
customer.Create(act.ID, act.AccountId.Value, act.AccountName, act.Referer);
// other instructions
}
catch (Exception ex)
{
Logging.Trace(ex);
}
In some cases, AccountId is null, and getting the Value like that threw an InvalidOperationException.
My question is : in a catch section, is this possible to retrieve the fact that AccountId was null ?
Using StackTrace or, something like that. I search on google but nothing appears.
As much as possible, I do not want to modify the structure of the code before, using if( Account != null) etc...
Thanks in advance for any kind of help.
In my C# project, I'm using System.Data.SQLite.dll downloaded from CodeProject.
My problem is as per the title - how to get the error codes after calling SqliteCommand.ExecuteNonQuery() function?
Error codes such as SQLITE_CONSTRAINT, SQLITE_BUSY, SQLITE_LOCKED as shown here.
use the Exception.StackTrace or the SQLiteException.ErrorCode
try
{
}
catch(SQLiteException ex)
{
string code = ex.ErrorCode;
}
I'm going to add to this to help others, if you're developing in .NET. Use the
SQLiteErrorCode enumeration to test the result, cast the ErrorCode:
try
{
}
catch(SQLiteException ex)
{
SQLiteErrorCode sqlLiteError= (SQLiteErrorCode)ex.ErrorCode;
//Do whatever logic necessary based of the error type
}
Good question.
System.Exception does not have a member by the name ".ErrorCode"
Catch Ex As SQLiteException
E = Ex.ResultCode
Return E
End Try
still giving problem
I have the following code. As long as I am in try { } it writes fine. But when there is an error, it doesn't write to log file. Not sure why
private static void jk(string kName, string path)
{
Job job;
try
{
// run some functions here and then write to the file
StreamWriter LJ = new StreamWriter("C:\\Lob.txt");
LJ.WriteLine("XXXXXXXXXXXX");
LJ.Close();
}
catch (InvalidException)
{
StreamWriter LJ = new StreamWriter("C:\\Lob.txt");
LJ.WriteLine("YYYYYYYYYYYYYYYY");
LJ.Close();
Console.WriteLine("Error: ");
return;
}
}
Because the only thing in your try is writing to the stream... and that's the same thing you try to do in the cacth. Why would that work?
The catch block executes only when the try block throws the exception (which appears to be a typo in the original post).
If the try succeeds, the catch is never executed.
If the try fails, it's because of a problem that must have occurred in writing to the log. When the catch executes, that problem most likely still exists, so the log within the catch will fail also.
Well, I don't know what type LJ is, and I certainly have never heard of a IncalidException. I am assuming that you just typed the code into the editor incorrectly. You should really just paste it in to avoid those types of errors.
Anyway, there are a few options:
LJ.WriteLine is not throwing an exception.
LJ.WriteLine is throwing an exception, but not of the same type you are catching (i.e., see if it works when you just catch { }).
The second call to LJ.WriteLine is also throwing an exception and you are catching (and perhaps swallowing) it further up the stack.
With your comment:
try fails because of some other problems but I am trying to log it
into the file
I assume that the exception is not thrown by LJ.WriteLine("XXXXXXXXXXXX");
If that's the case, you might just need to flush the StreamWriter. Try declaring LJ in a using block like this:
using (StreamWriter LJ = new StreamWriter("C:\\Lob.txt"))
{
LJ.WriteLine("XXXXXXXXXXXX");
try
{
...
LJ.WriteLine("XXXXXXXXXXXX");
}
catch (InvalidException)
{
LJ.WriteLine("YYYYYYYYYYYYYYYY");
Console.WriteLine("Error: ");
return;
}
}
Are you able to compile this code?
There are two things I see incorrect with the above.
It should be InvalidException not IncalidException
try
{
LJ.WriteLine("XXXXXXXXXXXX");
}
catch (InvalidException e)
{
LJ.WriteLine("YYYYYYYYYYYYYYYY");
Console.WriteLine("Error: {0}", e.Message);
return;
}
this is a really basic question (I hope). Most of the exception handling I have done has been with c#. In c# any code that errors out in a try catch block is dealt with by the catch code. For example
try
{
int divByZero=45/0;
}
catch(Exception ex)
{
errorCode.text=ex.message();
}
The error would be displayed in errorCode.text. If I were to try and run the same code in php however:
try{
$divByZero=45/0;
}
catch(Exception ex)
{
echo ex->getMessage();
}
The catch code is not run. Based on my limeted understanding, php needs a throw. Doesn't that defeat the entire purpose of error checking? Doesn't this reduce a try catch to an if then statement?
if(dividing by zero)throw error
Please tell me that I don't have to anticipate every possible error in a try catch with a throw. If I do, is there anyway to make php's error handling behave more like c#?
You could also convert all your php errors with set_error_handler() and ErrorException into exceptions:
function exception_error_handler($errno, $errstr, $errfile, $errline )
{
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
$a = 1 / 0;
} catch (ErrorException $e) {
echo $e->getMessage();
}
PHP's try-catch was implemented later in the language's life, and so it only applies to user-defined exceptions.
If you really want to handle actual errors, set your own error handler.
To define and catch exceptions:
function oops($a)
{
if (!$a) {
throw new Exception('empty variable');
}
return "oops, $a";
}
try {
print oops($b);
} catch (Exception $e) {
print "Error occurred: " . $e->getMessage();
}
From http://php.net/manual/en/language.exceptions.php
"Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException."
See also http://www.php.net/manual/en/class.errorexception.php
I think the only way to deal with this in PHP is to write:
try
{
if ($b == 0) throw new Exception('Division by zero.');
$divByZero = $a / $b;
}
catch(Exception ex)
{
echo ex->getMessage();
}
Unlike in C#, not every issue will raise an exception in PHP. Some issues are silently ignored (or not silently - they print something to the output), but there are other ways to handle these. I suppose this is because exceptions were not a part of the language since the first version, so there are some "legacy" mechanisms.