[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

exception

can
we use all exceptions available in Java
lik try ,catach so , similarly in  C++
0
ravndra
Asked:
ravndra
1 Solution
 
AlexVirochovskyCommented:
Yes, you can ! Example:
//***************************************************************
// Win32 Programming for Windows NT
//
// Sample
// by Johannes Plachy 1998/1999
// JPlachy@qualityservice.com
//
//***************************************************************


#include <windows.h>
#include <stdio.h>

/* exception filter function */
DWORD filter()
{
   printf("Hello");
   return EXCEPTION_EXECUTE_HANDLER;
}

void main(void)
{

   LPSTR p;

   try
   {
      try
      {
         p = (LPSTR)VirtualAlloc(NULL, 4096, MEM_COMMIT, PAGE_READONLY);
         *p = 7;    /* will fail - read only! */
         exit(34);
      }
      catch(...)
      {
         Beep(2000, 100);
         Sleep(1000);
         printf(" ");
      }
   }
   except (filter())
   {
      Beep(1000, 100);
      Sleep(1000);
      printf("World\n");
   }
}

See more details in Windows Doc about catch,try,finally...
Alex
0
 
jkrCommented:
This is Win32 SEH, which is completely different to C++ exceptions...
0
 
mikeblasCommented:
> This is Win32 SEH, which is completely different to C++ exceptions...

So what? The question is nearly nonsensical, so nearly any answer is worth trying. Spin the wheel and try your luck!

..B ekiM
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jkrCommented:
>>So what?

So MS. In case you forgot, not every computer runs Windows....

BTW: This is the C++ area, Windows is next door ;-)

0
 
KangaRooCommented:
It would at least make sense to seperate and explain the two (C++ based exceptions) and Windows SEH...
As general as the question was put, I'd say C++ exceptions are in order.
0
 
mikeblasCommented:
> BTW: This is the C++ area, Windows is next door ;-)

People here very frequently ask (and answer) questions in the wrong order.

..B ekiM
0
 
mikeblasCommented:
And I frequently say "order" when I mean "area".

..B ekiM
0
 
SridharReddyCommented:
#include <string>
#include <iostream>
using namespace std;

int main()
{
//code line 1
//code line 2
//....
//....
// code likely to 'throw' an exception
// don't put try catch for the entire code.
try{
    //code line n
    //code line n+1
// if(exceptional condition)
{
     throw object;
//(object may be a real object or a basic data type.. I prefer a real object with info about the point of occurence, module, error code etc )

} // if

}// try
// nothing here between try & catch.
catch(class& E) // class is the type of 'object'. i.e. an int or the class name itself.
{
E.Log();
E.Notify();
E.WhatEver();
throw; // to the next layer or the OS if this is the last layer/module.
In this case it is the last layer as it is the main program.

}//catch code

// take care of all unknown exceptions
catch(...)
{
className object; // exception handler class.

object.Log("Unkown Exception");
throw object;
}

//code line n+100
//*....
....*//

}





0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now