Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to generate and handle exceptions (SEH).
How can I write a code that will lead to an "illegal instruction"-exception?
You can do something like this:
#include <Windows.h>
#include <cstdio>
::DWORD FilterFunction(::DWORD const _exception_code)
{
if(EXCEPTION_ILLEGAL_INSTRUCTION == _exception_code)
{
::std::printf("filtered\n");
return EXCEPTION_EXECUTE_HANDLER;
}
else
{
::std::printf("not filtered\n");
return EXCEPTION_CONTINUE_SEARCH;
}
}
int main()
{
__try
{
::std::printf("trying...\n");
::RaiseException
(
EXCEPTION_ILLEGAL_INSTRUCTION // exception id
, 0 // continuable exception
, 0 // args count
, nullptr // args
); // no arguments
::std::printf("finished trying\n");
}
__except(FilterFunction(GetExceptionCode()))
{
::std::printf("exception handled\n");
}
return 0;
}
trying...
filtered
exception handled
You can raise a structured exception in Windows with:
RaiseException(EXCEPTION_ILLEGAL_INSTRUCTION, EXCEPTION_NONCONTINUABLE, 0, nullptr);
Reference:
https://msdn.microsoft.com/en-us/library/ms680552%28VS.85%29.aspx
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
public async Task < IActionResult > Events(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest httpRequest,
ILogger log) {
ValidateResponse res = eventService.ValidateSchema();
if (res.Valid == false && res.Errors != null) {
string[] messages = new string[100];
int k = 0;
for (int i = 0; i < res.Errors.Count; i++) {
string value = res.Errors[i].ToString();
if (value.StartsWith("Invalid type")) {
messages[k++] = value.Substring(10);
continue;
}
}
return new OkObjectResult($ "No of events posted {cloudEvents.Count} and errors :{messages[k]}");
}
But the values in messages are not returned. I understand that its an array . But unable to figure out the ways to loop here in return all array values once.
Once option is to join the messages array to a string:
return new OkObjectResult($"No of events posted {cloudEvents.Count} and errors:{String.join(\",\", messages}");
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am coding for an arduino project and i came across this problem, can anyone help me out!
if(condition1){
//my codes here
}
if(condition2){
//my codes here
}
if(condition3){
//my codes here
}
......
if(condition100){
//my codes here
}
else{
my codes here
}
I want to check all my if conditions, and execute the codes if the conditions are true, and run the else statement only if none of the if condition become true.
Note that i cant use else if because i want to check all the if conditions, and if none are true i want to run the else
If conditions are not dependent on each other
You can use a Boolean flag which is set in any of your ifs.
bool noPathTaken = true;
if ( condition1 ) {
noPathTaken = false;
// ...
}
if ( condition2 ) {
noPathTaken = false;
// ...
}
// ...
if ( noPathTaken ) { // this would be your "else"
// ...
}
must set off all else flag in each test
bool elseFlag = 1;
if(condition1){
elseFlag = 0;
my codes here
}
if(condition2){
elseFlag = 0;
my codes here
}
if(condition3){
elseFlag = 0;
my codes here
}
......
if(condition100){
elseFlag = 0;
my codes here
}
if (elseFlag) {
my codes here
}
because else gets binded only into preceding test operation, here if(condition100) {
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Simple question.
I have this code in front of me:
if ( somethings() == false ) {
log("error when ...somethings()");
return false;
}
// do somethings
if ( somethings2() == false ) {
log("error when ...somethings2()");
return false;
}
// ...
// eventually
if ( somethingsN() == false ) {
log("error when ...somethingsN()");
delete myData;
return false;
}
Not sexy, right ? What would be the best method(/pattern ?) for improving this code ?
Solution 1:
I was wondering about using exceptions
try {
if ( somethings() == false )
throw std::runtime_error("error when ...somethings()");
// do somethings
if ( somethings2() == false )
throw std::runtime_error("error when ...somethings2()");
// ...
if ( somethingsN() == false )
throw std::runtime_error("error when ...somethingsN()");
}
catch ( std::exception& e ) {
log(e.what());
delete myData;
return false;
}
Solution 2:
Using nested if statements
if ( somethings() ) {
// do somethings...
if ( somethings1() ) {
// ... (eventually too many indent)
if ( somethingsN() ) {
return true;
}
}
}
delete myData;
return false;
What are your methods ?
I just want to improve my coding style here.
Note (edit)
"Do not use 'delete', use smart pointers instead such as unique_ptr" (thanks all)
Throw an exception on error rather than returning a magic value for the caller to check; avoid dynamic allocation except when you really need it; use smart pointers or other RAII types to manage it when you really do. Then the code becomes rather more straightforward:
somethings();
somethings2();
// ...
somethingsN();
#define CHECK(x) \
do { if (!(x)) throw MyException(#x, __FILE__, __LINE__); } while(0)
try {
// call third-party or
// C functions that don't throw
CHECK(thisone());
CHECK(thatone());
CHECK(theotherone());
// call my own functions
anotherone(); // throws
yetanotherone(); // throws
} catch (std::exception& e) {
log(e.what());
}
And of course no delete. unique_ptr is your friend.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
this code it lists all processes and all threads of the process, but I want it lists only the thread of a process by pid ... example: explorer.exe pid = 5454 through the pid wanted him to have the ids of threads and thread state.
How about passing the process ID as a command line argument to the program and filter out the content required.
while( pi )
{
SYSTEM_PROCESS_INFORMATION* next = PROCESS_INFORMATION_NEXT( pi );
UINT32 count, n;
if (argc > 1 && pi->UniqueProcessId == (HANDLE)atoi(argv[1])) {
printf("**************************************\n");
if( pi->ImageName.Buffer )
wprintf(L"%u %s <------ PROCESSO\r\n", pi->UniqueProcessId, pi->ImageName.Buffer);
else
wprintf(L"%u %s *\r\n", pi->UniqueProcessId, L"System Idle Process");
if( next )
count = ThreadCount( pi, (ULONG_PTR)next );
else
count = ThreadCount( pi, (ULONG_PTR)spi + size );
for( n=0; n<count; n++ )
{
SYSTEM_THREAD_INFORMATION* th = pi->Threads + n;
wprintf(L" [%u] StartAddress=%p ID Processo=%u IdThred=%u State=%u \r\n",
n+1, th->StartAddress, th->ClientId.UniqueProcess, th->ClientId.UniqueThread,th->WaitReason);
}
}
pi = next;
}
you can see this on msdn
i think this is what you are looking for.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
can someone explain me on what does these codes did? this is only part of the code...
// Create the window of the application
sf::RenderWindow myWindow(sf::VideoMode(myWorldWidth, myWorldHeight, 32), "Battleship!");
myWindow.setVerticalSyncEnabled(true);
bool showHardwareMouse;
bool started;
bool drag;
float dragOffsetX, dragOffsetY;
bool LeftMouseButtonDown = false;
bool reset = true;
//----- Main Loop Start here -----
while (myWindow.isOpen())
{
if (reset)
{
// Reset
showHardwareMouse = true;
drag = false;
dragOffsetX = dragOffsetY = 0.0f;
started = true;
reset = false;
}
the program is actually based on SFML library, what does dragOffsetX = dragOffsetY = 0.0f; means?
and this is how actually the program work like...
http://i1146.photobucket.com/albums/o530/HTHVampire/C%20plus%20plus/Capture2_zps1fe188cd.jpg
I will post the full codes of it if you guys can't get it. Thanks!
dragOffsetX = dragOffsetY = 0.0f;
is the same as
dragOffsetX = (dragOffsetY = 0.0f);
where an assignment a = b has the "value" b. So the above line is the same as:
dragOffsetX = 0.0f;
dragOffsetY = 0.0f;
The rest of the code consists mainly of declarations and initializations and should be obvious.