Error converting varchar to int? - sql-server-2008

Msg 245, Level 16, State 1, Line 89
Conversion failed when converting the varchar value 'There was an error creating the System.DirectoryServices assembly. ' to data type int.
This error is caused by the statement below:
begin try print ' - Preparing to create System.DirectoryServices assembly with UNSAFE_ACCESS permission'
create assembly [System.DirectoryServices] from 'c:\Windows\Microsoft.NET\Framework\v2.0.50727\System.DirectoryServices.dll'
with permission_set = unsafe
print ' - System.DirectoryServices assembly created'
end try
begin catch
print 'There was an error creating the System.DirectoryServices assembly. '+Error_number()+Error_Severity()
end catch

This is coming from your concatenation of number to string in the catch block. This is not allowed without casting as it tries to convert the string to number rather than vice versa and as your string literal is not numeric this attempt is doomed to fail!
You can use RAISERROR with severity 0 to print the message with the values substituted in or the alternative is to concatenate the string yourself using explicit casts.
begin catch
declare #num int = Error_number()
declare #sev int = Error_Severity()
raiserror('There was an error creating the System.DirectoryServices assembly. %d %d ',0,1, #num,#sev)
end catch

Related

Error in using pcap loop with mysql con as My argument

I want to add the mysql connection with the pcap loop which I am using in the code
MYSQL *con;
u_char *my_arguments = con;
pcap_loop(handle, total_packet_count, my_packet_handler, my_arguments);
but it is giving error
warning: initialization from incompatible pointer type
[-Wincompatible-pointer-types]
u_char *my_arguments = con;^~~
SO what should I do help is needed please
When I am directly putting value of con in pcap loop like pcap_loop(handle, total_packet_count, my_packet_handler, con); it is showing new error
Error is
passing argument 4 of ‘pcap_loop’ from incompatible pointer type
[-Wincompatible-pointer-types]
pcap_loop(handle, total_packet_count, my_packet_handler, con);
note: expected ‘u_char * {aka unsigned char *}’ but argument is of type ‘MYSQL * {aka struct st_mysql *}’
PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
but I want it to be pushed in pcap loop
The "user" argument to pcap_loop(), pcap_dispatch(), and the callback probably should have been defined to be a void *, as it's a pointer to some arbitrary data that the callback understands, but it was defined to be a u_char * instead.
So you should cast the MYSQL * to u_char *:
pcap_loop(handle, total_packet_count, my_packet_handler, (u_char *)con);

Dec Ada & exception

I have this code in one modules:
PROCEDURE Get (File: IN Ada.Text_IO.File_Type; Item : OUT Rational) IS
N: Integer;
D: Integer;
Dummy: Character;
BEGIN -- Get
LOOP
BEGIN
Ada.Integer_Text_IO.Get(File => File, Item => N);
Ada.Text_IO.Get (File => File, Item => Dummy);
Ada.Integer_Text_IO.Get(File => File, Item => D);
Item := N/D;
if Dummy /= '/' then
........;
end if;
EXIT;
EXCEPTION
when other =>
Ada.Text_IO.Put_Line(" here is exception ");
END;
END LOOP;
END Get;
What is differences with this second code.
The main of my question is if I don't put raise in body of statement of exception what is happen?
PROCEDURE Get (File: IN Ada.Text_IO.File_Type; Item : OUT Rational) IS
N: Integer;
D: Integer;
Dummy: Character;
BEGIN -- Get
LOOP
BEGIN
Ada.Integer_Text_IO.Get(File => File, Item => N);
Ada.Text_IO.Get (File => File, Item => Dummy);
Ada.Integer_Text_IO.Get(File => File, Item => D);
Item := N/D;
if Dummy /= '/' then
........;
end if;
EXIT;
EXCEPTION
when other =>
Ada.Text_IO.Put_Line(" here is exception ");
**raise;**
END;
END LOOP;
END Get;
The main of my question is if I don't put raise in body of statement of exception what is happen???
Thank you very much.
The only difference between both code modules is that the exception (if any is raised during execution of Get) is reraised, i.e. the exception is propagated to the caller of Get.
Whether this is the desired behaviour depends on your needs, i.e. does the caller of Get need to know that an exception occurred?
In your example several kinds of exceptions may occur, e.g.
not reading the correct/expected input (the file to read from does not start with a number)
trying to read from a file that has not been opened
reading D as 0 (thus resulting in a division by 0)
All of these are handled in the same manner by printing "here is exception". The first implementation of Get then silently returns control to the caller (who will not know that anything strange happened). The second implementation, however, will inform the caller by reraising the exception.
For more information see the Ada LRM §11.3 (Raise Statements).

number(X) as function parameter or return value generates PLS 00103 error

I have the following function definition:
create or replace FUNCTION checkXML
(idx in number(19))
return number(19)
is
...
But when I compile it, am getting the following errors,
Error(2,16): PLS-00103: Encountered the symbol "(" when expecting one of the
following: := . ) , # % default character The symbol ":=" was substituted for
"(" to continue.
Error(3,15): PLS-00103: Encountered the symbol "(" when expecting one of the
following: . # % ; is authid as cluster order using external character
deterministic parallel_enable pipelined aggregate result_cache
Change the function declaraction to be
create or replace FUNCTION checkXML
(idx in number)
return number
PL/SQL doesn't accept length or precision specifiers on parameters or return types.
Share and enjoy.

Defining and Catching Exceptions

I'm still getting the hang of haskell, and am trying to build my first "real" coding project, i.e. one where I'm going to including testing, write up documentation, etc. I'm sufficiently new to haskell where I know I don't have a lot of the knowledge of the language necessary so that everything I want to do is immediately within reach, but that's kind of the idea, so that the act of finishing will require that I touch most of the major pieces of the language.
Anyway, so the current issue I'm having is regarding throwing and catching exceptions in the language, something that I understand can be done with quite varied approaches. I have a function here, toLower:
toLower :: String -> String
toLower plaintext =
if (catch (nonAlpha plaintext) handlerNonAlpha)
then map charToLower plaintext
else exitFailure
Which will take a string, throw an exception and exit if the string includes any non-alpha characters (so if not A-Z or a-z), or if not convert the string to lowercase. So what I have for the nonAlpha function is:
--- detect non-alpha character - throw error if existant
data NonNumericException = NonNumException
instance Exception NonNumericException
handlerNonAlpha :: NonNumericException -> IO()
handlerNonAlpha ex =
putStrLn "Caught Exception: " ++ (show ex) ++ " - A non-alpha character was included in the plaintext."
nonAlpha :: String -> Bool
nonAlpha str =
let nonalphas = [x | x <- str, (ord x) < 65 || (90 < (ord x) && (ord x) < 97) || 123 < (ord x)]
in if (length nonalphas) == 0
then True
else throw NonNumException
As I said I'm pretty new to haskell, so I'm a little vague on how this data/instance structure works, but as I understand it I'm defining an parent NonNumericException, of which NonNumException is a child (and I could have more), and in the instance line defining them to be Exceptions. The catch structure, if it detects an exception (for instance, when one is thrown at the end of nonAlpha if there is a non-alpha character), then calls the handler.
So here are the compile errors that I get:
utilities.hs:61:3:
Couldn't match expected type `[Char]' with actual type `IO ()'
In the return type of a call of `putStrLn'
In the first argument of `(++)', namely
`putStrLn "Caught Exception: "'
In the expression:
putStrLn "Caught Exception: "
++
(show ex)
++ " - A non-alpha character was included in the plaintext."
utilities.hs:61:3:
Couldn't match expected type `IO ()' with actual type `[Char]'
In the expression:
putStrLn "Caught Exception: "
++
(show ex)
++ " - A non-alpha character was included in the plaintext."
In an equation for `handlerNonAlpha':
handlerNonAlpha ex
= putStrLn "Caught Exception: "
++
(show ex)
++ " - A non-alpha character was included in the plaintext."
utilities.hs:73:7:
Couldn't match expected type `Bool' with actual type `IO ()'
In the return type of a call of `catch'
In the expression: (catch (nonAlpha plaintext) handlerNonAlpha)
In the expression:
if (catch (nonAlpha plaintext) handlerNonAlpha) then
map charToLower plaintext
else
exitFailure
utilities.hs:73:14:
Couldn't match expected type `IO ()' with actual type `Bool'
In the return type of a call of `nonAlpha'
In the first argument of `catch', namely `(nonAlpha plaintext)'
In the expression: (catch (nonAlpha plaintext) handlerNonAlpha)
utilities.hs:75:8:
Couldn't match type `IO a0' with `[Char]'
Expected type: String
Actual type: IO a0
In the expression: exitFailure
In the expression:
if (catch (nonAlpha plaintext) handlerNonAlpha) then
map charToLower plaintext
else
exitFailure
In an equation for `toLower':
toLower plaintext
= if (catch (nonAlpha plaintext) handlerNonAlpha) then
map charToLower plaintext
else
exitFailure
So I guess my two question are, a) what's going wrong with the types for the handler (the line 61 errors), and b) how do I properly set the types for the functions that may throw an exception or exit with failure, but otherwise will return a bool or a string?
EDIT: I guess I should note. I do see the similarities between this question and a number of others that have been asked. Part of what I'm looking for that I don't see is a description of what the structures here are actually doing, and what is best practice and why.
What is best practice in Haskell is to leverage the awesome power of its type system to avoid needing to throw/catch exceptions for pure functions. There are cases where throwing an exception can actually make sense, but for something like your toLower function you can just choose to have a different return type. For example:
-- We can factor out our check for a non-alpha character
isNonAlpha :: Char -> Bool
isNonAlpha c = c' < 65 || (90 < c' && c' < 97) || 123 < c'
where c' = ord c
-- Why throw an exception? Just return False
hasNonAlpha :: String -> Bool
hasNonAlpha str = any isNonAlpha str
-- Renamed to not conflict with Data.Char.toLower
myToLower :: String -> Maybe String
myToLower plaintext =
if hasNonAlpha plaintext
then Nothing
else Just $ map toLower plaintext
Not only is this cleaner code, but now we don't have to worry about error handling at all, and someone else using your code won't get a nasty surprise. Instead, the notion of failure is encoded at the type level. To use this as an "error handling" mechanism, just work in the Maybe monad:
doSomething :: String -> String -> Maybe String
doSomething s1 s2 = do
s1Lower <- myToLower s1
s2Lower <- myToLower s2
return $ s1Lower ++ s2Lower
If either myToLower s1 or myToLower s2 returns Nothing, then doSomething will return Nothing. There is no ambiguity, no chance for an unhandled exception, and no crashing at runtime. Haskell exceptions themselves, those thrown by the function throw, must be caught by catch, which has to execute in the IO monad. Without the IO monad, you can't catch exceptions. In pure functions, you can always represent the concept of failure with another data type without having to resort to throw, so there is no need to over-complicate code with it.
Alternative, you could have even written myToLower monadically as
import Control.Monad
-- Other code
myToLower :: String -> Maybe String
myToLower plaintext = do
guard $ not $ hasNonAlpha plaintext
return $ map toLower plaintext
The guard from Control.Monad acts as a sort of filter for MonadPlus instances. Since Maybe is an instance of MonadPlus (as are lists), this gives us very simple code.
Or, if you want to pass around an error message:
type MyError = String
myToLower :: String -> Either MyError String
myToLower plaintext = if hasNonAlpha plaintext
then Left $ "The string " ++ plaintext ++ " has non-alpha character(s)"
else Right $ map toLower plaintext
Then you can change the type of doSomething to match:
doSomething :: String -> String -> Either MyError String
doSomething s1 s2 = do
s1Lower <- myToLower s1
s2Lower <- myToLower s2
return $ s1Lower ++ s2Lower
If you notice, the monadic syntax lets us change the type signature of our function without even having to change the code! Play around with this implementation to get a feel for how it works.
Learning about exceptions is useful, and they are great for handling exceptional circumstances.
The best place to read about exceptions is Simon Marlow's paper, An Extensible Dynamically-Typed Heirarchy of Exceptions. His book, Parallel Concurrent Programming in Haskell is another good resource on their use.
The following are a few comments on your question.
error on line 61
handlerNonAlpha :: NonNumericException -> IO()
handlerNonAlpha ex =
putStrLn "Caught Exception: " ++ (show ex) ++ ...
Function arguments are consumed eagerly in haskell. You'll have to modify this line as follows to perform string concatenation before calling putStrLn:
putStrLn $ "Caught Exception: " ++ (show ex) ++ ...
comment on nonAlpha
Exceptions can only be caught from an IO computation, and it's best to avoid throwing them from pure functions. Besides this, the problem with nonAlpha is that it claims to return a Bool, but actually returns either True or throws an exception. Why not just return False?
nonAlpha :: String -> Bool
nonAlpha str =
let nonalphas = [x | x <- str, (ord x) < 65 || (90 < (ord x) && (ord x) < 97) || 123 < (ord x)]
in if (length nonalphas) == 0
then True
else False
Pull your exception throwing code out of nonAlpha like so. The name of this function and its lack of return value indicate that it might throw an exception:
trapInvalid :: String -> IO ()
trapInvalid str = unless (nonAlpha str) $ throw NonNumException

Passing 2d array to function in Lua

It is possible to pass 2d array to a function as a paramter ?
I initialized an array like this :
tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= 0
end
end
and i have function like this :
function foo(data)
...
x = data[i][z] -- here i got error
...
end
The gave the error message attempt to index field '?' (a nil value)
All variables are declared and initialized.
Your code should work if it is initialized properly.
For example, the below code sample will output 3:
function foo(data)
local i, z = 1, 2
print(data[i][z])
end
local tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= i + z
end
end
foo(tab)
Maybe you can share the rest of your code? The following runs with no error:
tab={}
for i=1, 10 do
tab[i]={}
for z=1, 10 do
tab[i][z]= 0
end
end
function foo(data)
print(data[3][2])
end
foo(tab)
The gave the error message attempt to index field '?' (a nil value)
I got such errors while changing metatable of some variable.