fjm 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. - $ex = new Exception('My message');
-
try
-
{
-
foreach ($clients as $_client)
-
{
-
try
-
{
-
// execute some code here
-
}
-
catch (Exception $ex)
-
{
-
throw $ex;
-
}
-
}
-
// execute something here
-
}
-
catch (Exception $ex)
-
{
-
throw $ex;
-
}
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…) - try {
-
// …
-
if (requirement not met)
-
{
-
throw new Exception($msg, $code);
-
}
-
}
-
catch (Exception $e)
-
{
-
logException($e);
-
// do something to put things right
-
}
you may re-throw an Exception in the catch block, although you should only do that, if necessary (otherwise consider catching the Exception later) - try {
-
// in a subfunction the Exception may be thrown
-
myfunction();
-
}
-
catch (Exception $e)
-
{
-
// put things right
-
}
PS: consider using the ErrorException class, there you can specify the severity level.
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?
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* - try {
-
// your code as is
-
}
-
catch (Exception $e)
-
{
-
// echo message
-
echo $e->getMessage(), "\n\n", $e->getTraceAsString();
-
// abort script for now
-
exit;
-
}
you may also want to set a default exception handler ( set_exception_h andler())
* what a pun…
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. :) - $ex = new Exception('An error message');
-
try
-
{
-
foreach ($xdop as $_xdop)
-
{
-
$page = GenerateRpt($_xdop, $rptop);
-
$message = new Message("$title", "$page");
-
-
try
-
{
-
$message->add($_xdop['email']);
-
}
-
catch (complianceException $ex)
-
{
-
$message->from("$email");
-
$mailer = new Mailer(Transport::newInstance("$host"));
-
$mailer->send($message);
-
}
-
-
$message->from("$email");
-
$mailer = new Mailer(Transport::newInstance("$host"));
-
$mailer->send($message);
-
}
-
$message->From("$email");
-
$mailer = new Mailer(Transport::newInstance("$host"));
-
$mailer->send($message);
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!
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)
fjm 348
Contributor
Thanks Dormilich. I'll check back in the morning, well... the afternoon. It wound up to be another late night. :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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.
|
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?...
|
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...
| |
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
|
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:
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |