473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with exceptions

348 Contributor
Hello everyone,

Can someone please give me a hand with an exception? I cannot seem to get this to work correctly or I just don't fully understand exceptions.

It's my assumption that I should be able to trap an exception, log that exception and have the code continue, not halt. What happens is that my code just stops dead instead of continuing.

I don't have a subclass to the exception class and maybe that is the problem. Can someone please shine a light on this for me?

Thanks.

Here is what I have so far.

Expand|Select|Wrap|Line Numbers
  1.     $ex = new Exception('My message');
  2.     try
  3.     {
  4.       foreach ($clients as $_client)
  5.       {
  6.         try
  7.         {
  8.           // execute some code here
  9.         }
  10.         catch (Exception $ex)
  11.         {
  12.           throw $ex;
  13.         }
  14.       }
  15.     // execute something here
  16.     }
  17.     catch (Exception $ex)
  18.     {
  19.       throw $ex;
  20.     }
Apr 5 '09 #1
7 1350
Dormilich
8,658 Recognized Expert Moderator Expert
@fjm
that's because you throw the Exception inside the catch block (the catch block catches an Exception thown in the according try block), where it isn't caught. this should result in a "uncaught exception" error.

usually you throw the (primary) exception in the try block. the catch block is there to correct things, that may be corrupted by the failing code (i.e. tell the user that today the DB doesn't work…)
Expand|Select|Wrap|Line Numbers
  1. try {
  2.     // …
  3.     if (requirement not met)
  4.     {
  5.         throw new Exception($msg, $code);
  6.     }
  7. }
  8. catch (Exception $e)
  9. {
  10.     logException($e);
  11.     // do something to put things right
  12. }
you may re-throw an Exception in the catch block, although you should only do that, if necessary (otherwise consider catching the Exception later)
Expand|Select|Wrap|Line Numbers
  1. try {
  2.     // in a subfunction the Exception may be thrown
  3.     myfunction();
  4. }
  5. catch (Exception $e)
  6. {
  7.     // put things right
  8. }
PS: consider using the ErrorException class, there you can specify the severity level.
Apr 5 '09 #2
fjm
348 Contributor
Hi Dormilich, thanks for the hand and the great examples. I've not used exceptions until now because I have a block of code that is really important to know if something goes wrong with.

The exception you see in the catch block was not originally in there, I put it there while trying to follow a tutorial on try/catch. Even with the exception removed, the code still stops dead and I need it to keep going. I also need to be made aware of the error whether that is through an error log or some other method.

What am I doing wrong that is making the code halt?
Apr 5 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
I guess there is an Exception thrown in your code (but it's hard to tell without seeing it)

try*
Expand|Select|Wrap|Line Numbers
  1. try {
  2. // your code as is
  3. }
  4. catch (Exception $e)
  5. {
  6. // echo message
  7.     echo $e->getMessage(), "\n\n", $e->getTraceAsString();
  8. // abort script for now
  9.     exit;
  10. }
you may also want to set a default exception handler (set_exception_h andler())


* what a pun…
Apr 5 '09 #4
fjm
348 Contributor
Thanks again for the hand.

Here is what I now have. It is not halting anymore but I am not getting any errors either in my log.

Just to test to see if I could get this thing to work, I copied the block of code to three different places. It's working, but I need to get this right.

Thanks again for the help. Can you please tell me what I need to do to get this correct? I understand the principle behind exceptions but I'm having a hard time putting it into practice. One thing is certain... The last three lines in the code are incorrect because they need to be in the foreach loop so that narrows it down a little. :)

Expand|Select|Wrap|Line Numbers
  1.     $ex = new Exception('An error message');
  2.     try
  3.     {
  4.       foreach ($xdop as $_xdop)
  5.       {
  6.         $page = GenerateRpt($_xdop, $rptop);
  7.         $message = new Message("$title", "$page");
  8.  
  9.         try
  10.         {
  11.           $message->add($_xdop['email']);
  12.         }
  13.         catch (complianceException $ex)
  14.         {
  15.           $message->from("$email");
  16.           $mailer = new Mailer(Transport::newInstance("$host"));
  17.           $mailer->send($message);
  18.         }
  19.  
  20.         $message->from("$email");
  21.         $mailer = new Mailer(Transport::newInstance("$host"));
  22.         $mailer->send($message);
  23.       }
  24.       $message->From("$email");
  25.       $mailer = new Mailer(Transport::newInstance("$host"));
  26.       $mailer->send($message);
Apr 5 '09 #5
fjm
348 Contributor
Dormilich,

I'm curious.. Is set_exception_h andler() to be used in place of a myException class that would extend php's Exception class or would I use both of them?

Thanks again!
Apr 5 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
@fjm
set_exception_h andler() is used to catch uncaught exceptions

PS: I'll look into your code later, but there is a catch block missing (in the posted code)
Apr 5 '09 #7
fjm
348 Contributor
Thanks Dormilich. I'll check back in the morning, well... the afternoon. It wound up to be another late night. :)
Apr 5 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2041
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
5
2111
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really appreciate his help. thanks for anybody in advance
1
2023
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is the last one on program.cpp subsetsum(int aList, int sum)so please can you help.
2
3507
by: felecha | last post by:
I learned about Exceptions in School, and now that I'm building my first real application on the job, I'm going through it trying to analyze it for all possible Exceptions so I can handle them effectively. I've been looking in the Help / Index for each Method's documentation, and looking at the listed Exceptions. I'm finding a number of methods with no Exceptions specified. Does that mean there is no possible Exception for that method?...
4
4786
by: Martin Odhelius | last post by:
Hello, I have a very fustrating problems while debugging ASP.NET applications. When I am debugging and stepping thru code (with F10 or F11) Visual Studio some times suddenly just stop responding and the only way get it up and running again is to kill the aspnet_wp.exe process. When the debugger hangs there is almost no CPU load, so it doesn't seem to be any hidden eternal loop or anything like that. I have tried to reinstall IIS and...
33
2423
by: aaron | last post by:
I have a question in my class.. hoping to get some help I need to create a program that will print firstName middleName lastName then their initials User will input: John Smith Doe Output: John
1
1050
by: rupinderbatra | last post by:
Hello, I have a table in MS-ACCESS containing the data in the following format: ID JobName SetDate Status 1021 Stock Fraser 9/21/2007 Job Completed with Exceptions 1022 Cadisch Backup 9/20/2007 Job Success 1023 Stock Fraser 9/20/2007 Job Success 1024 SGD Daily Backup 9/21/2007 Job Failed 1025 Thurleigh Daily 9/19/2007 Job Completed with Exceptions I want to get an output in the format:
0
6501
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like NullPointerException or ArrayIndexOutOfBoundsException. All of these extend the basic class Exception. In general, you can sort Exceptions into two groups: Checked and unchecked Exceptions. Checked Exceptions are checked by the compiler at compilation time. Most...
32
3248
by: ndedhia1 | last post by:
Right now, I am using this line of code to get rid of data in a file called alarmNotification that I do not want: egrep "low debug.*\".*\"" $dbDir/alarmNotification.log but I am having some problem. An entry like the one below in the alarmNotification.log file is getting deleted when I only want a part of the entry to be deleted, keeping the rest of the line in tact low debug 2009/3/9 8:30:21.01 ICSNotificationAlarm...
2
10045
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.