Skip to content

Can we support exclude some specific Exception when catch Exception? #11228

@gitHusband

Description

@gitHusband

Description

Hi,

Can we support exclude some specific Exception when catch Exception?

I don't want to catch RuntimeException in Child catching block, but catch RuntimeException in Parent catching block.
(NOTE that RuntimeException just an example, not real)

For now, I can handle it like test and test1 method.

But can we support any other more concise approach? such as test2 method?

For example,

<?php

class Example
{
    public function test()
    {
        // Parent catching
        try {
            // Child catching
            try {
                // Throw Exception
            } catch (\Exception $e) {
                // exclude \RuntimeException and re-throw it
                if ($e instanceof \RuntimeException) 
                    throw $e;
                
                return false;
            }
        } catch (\RuntimeException $e) {
            return true;
        }
    }

    public function test1()
    {
        // Parent catching
        try {
            try {
                // Throw Exception
            } catch (\RuntimeException $e) {
                // exclude \RuntimeException and re-throw it
                throw $e;
            } catch (\Exception $e) {
                return false;
            }
        } catch (\RuntimeException $e) {
            return true;
        }
    }

    public function test2()
    {
        // Parent catching
        try {
            // Child catching
            try {
                // Throw Exception
            } 
            // catch \Exception but exclude \RuntimeException
            catch (\Exception && !\RuntimeException $e) {
                return false;
            }
        } catch (\RuntimeException $e) {
            return true;
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions